From 724855c7da07a8519ca1068cceb7d8d380f51815 Mon Sep 17 00:00:00 2001 From: Yi Wu Date: Tue, 29 May 2018 15:11:22 -0700 Subject: [PATCH] Fix LRUCache missing null check on destruct Summary: Fix LRUCache missing null check on destruct. The check is needed if LRUCache::DisownData is called. Closes https://github.com/facebook/rocksdb/pull/3920 Differential Revision: D8191631 Pulled By: yiwu-arbug fbshipit-source-id: d5014f6e49b51692c18a25fb55ece935f5a023c4 --- cache/lru_cache.cc | 10 +++++++--- cache/lru_cache.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cache/lru_cache.cc b/cache/lru_cache.cc index a128296f9..432fee66d 100644 --- a/cache/lru_cache.cc +++ b/cache/lru_cache.cc @@ -474,10 +474,13 @@ LRUCache::LRUCache(size_t capacity, int num_shard_bits, } LRUCache::~LRUCache() { - for (int i = 0; i < num_shards_; i++) { - shards_[i].~LRUCacheShard(); + if (shards_ != nullptr) { + assert(num_shards_ > 0); + for (int i = 0; i < num_shards_; i++) { + shards_[i].~LRUCacheShard(); + } + port::cacheline_aligned_free(shards_); } - port::cacheline_aligned_free(shards_); } CacheShard* LRUCache::GetShard(int shard) { @@ -504,6 +507,7 @@ void LRUCache::DisownData() { // Do not drop data if compile with ASAN to suppress leak warning. #ifndef __SANITIZE_ADDRESS__ shards_ = nullptr; + num_shards_ = 0; #endif // !__SANITIZE_ADDRESS__ } diff --git a/cache/lru_cache.h b/cache/lru_cache.h index f5219ad35..3c067f0c1 100644 --- a/cache/lru_cache.h +++ b/cache/lru_cache.h @@ -295,7 +295,7 @@ class LRUCache : public ShardedCache { double GetHighPriPoolRatio(); private: - LRUCacheShard* shards_; + LRUCacheShard* shards_ = nullptr; int num_shards_ = 0; };