From 2aba0a228324b2868e62df074fac616d7cc4e609 Mon Sep 17 00:00:00 2001 From: Fabien Sanglard Date: Wed, 14 Jun 2023 23:08:02 +0000 Subject: [PATCH] Fix LruCache, allow std:string caching The default initalization for mNullValue uses 0 which is in the case of a std::string TValue will invoke the contructor with undefined behavior parameter. Using an empty uniform initialization {} addresses the problem. Test: Already tested in lrucache_test.cpp Bug: 257127748 Change-Id: I37420ce8a16c99f3014538a0208d7e113870b1c7 --- libutils/LruCache_test.cpp | 11 +++++++---- libutils/include/utils/LruCache.h | 12 ++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/libutils/LruCache_test.cpp b/libutils/LruCache_test.cpp index 8b16947be..5cd3cbb90 100644 --- a/libutils/LruCache_test.cpp +++ b/libutils/LruCache_test.cpp @@ -29,6 +29,8 @@ typedef const char* StringValue; struct ComplexKey { int k; + explicit ComplexKey() : k(0) { instanceCount += 1; } + explicit ComplexKey(int k) : k(k) { instanceCount += 1; } @@ -57,6 +59,8 @@ ssize_t ComplexKey::instanceCount = 0; struct ComplexValue { int v; + explicit ComplexValue() : v(0) { instanceCount += 1; } + explicit ComplexValue(int v) : v(v) { instanceCount += 1; } @@ -83,10 +87,9 @@ struct KeyWithPointer { struct KeyFailsOnCopy : public ComplexKey { public: - KeyFailsOnCopy(const KeyFailsOnCopy& key) : ComplexKey(key) { - ADD_FAILURE(); - } - KeyFailsOnCopy(int key) : ComplexKey(key) { } + KeyFailsOnCopy() : ComplexKey() {} + KeyFailsOnCopy(const KeyFailsOnCopy& key) : ComplexKey(key) { ADD_FAILURE(); } + KeyFailsOnCopy(int key) : ComplexKey(key) {} }; } // namespace diff --git a/libutils/include/utils/LruCache.h b/libutils/include/utils/LruCache.h index b4243a3db..70901b63b 100644 --- a/libutils/include/utils/LruCache.h +++ b/libutils/include/utils/LruCache.h @@ -161,12 +161,12 @@ public: // Implementation is here, because it's fully templated template LruCache::LruCache(uint32_t maxCapacity) - : mSet(new LruCacheSet()) - , mListener(nullptr) - , mOldest(nullptr) - , mYoungest(nullptr) - , mMaxCapacity(maxCapacity) - , mNullValue(0) { + : mSet(new LruCacheSet()), + mListener(nullptr), + mOldest(nullptr), + mYoungest(nullptr), + mMaxCapacity(maxCapacity), + mNullValue{} { mSet->max_load_factor(1.0); };