Skip to content

Commit aeddea3

Browse files
authored
Fix crash in DefaultCache::Put with nullptr value (#1050)
DefaultCache::Put value is ValueTypePtr. If the value is nullptr the crash will occur. Adding check for the value and unit test. Resolves: OLPEDGE-2295 Signed-off-by: Kostiantyn Zvieriev <ext-kostiantyn.zvieriev@here.com>
1 parent 4afb3d3 commit aeddea3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ bool DefaultCacheImpl::Put(const std::string& key, const boost::any& value,
205205
bool DefaultCacheImpl::Put(const std::string& key,
206206
const KeyValueCache::ValueTypePtr value,
207207
time_t expiry) {
208+
if (!value) {
209+
return false;
210+
}
211+
208212
std::lock_guard<std::mutex> lock(cache_lock_);
209213
if (!is_open_) {
210214
return false;

olp-cpp-sdk-core/tests/cache/DefaultCacheTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ void BasicCacheTestWithSettings(const olp::cache::CacheSettings& settings) {
6868
EXPECT_EQ(*data_read, binary_data);
6969
}
7070

71+
{
72+
SCOPED_TRACE("Put nullptr value");
73+
74+
olp::cache::DefaultCache cache(settings);
75+
ASSERT_EQ(olp::cache::DefaultCache::Success, cache.Open());
76+
ASSERT_TRUE(cache.Clear());
77+
78+
bool put_result =
79+
cache.Put("key", nullptr, (std::numeric_limits<time_t>::max)());
80+
EXPECT_FALSE(put_result);
81+
}
82+
7183
{
7284
SCOPED_TRACE("Remove from cache");
7385

0 commit comments

Comments
 (0)