]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
kv/rocksdb_cache: Remove C style struct hack undefined behavior 25508/head
authorMark Nelson <mnelson@redhat.com>
Fri, 7 Dec 2018 19:02:52 +0000 (13:02 -0600)
committerMark Nelson <mnelson@redhat.com>
Wed, 12 Dec 2018 17:28:23 +0000 (11:28 -0600)
Signed-off-by: Mark Nelson <mnelson@redhat.com>
src/kv/rocksdb_cache/BinnedLRUCache.cc
src/kv/rocksdb_cache/BinnedLRUCache.h

index 217f0c1dcffe4f1fec36333eb5ea70a76fa92a4d..91ed1185ec92a600cc24955edf7a89a1f0b14209 100644 (file)
@@ -342,11 +342,7 @@ rocksdb::Status BinnedLRUCacheShard::Insert(const rocksdb::Slice& key, uint32_t
                              size_t charge,
                              void (*deleter)(const rocksdb::Slice& key, void* value),
                              rocksdb::Cache::Handle** handle, rocksdb::Cache::Priority priority) {
-  // Allocate the memory here outside of the mutex
-  // If the cache is full, we'll have to release it
-  // It shouldn't happen very often though.
-  BinnedLRUHandle* e = reinterpret_cast<BinnedLRUHandle*>(
-      new char[sizeof(BinnedLRUHandle) - 1 + key.size()]);
+  auto e = new BinnedLRUHandle();
   rocksdb::Status s;
   ceph::autovector<BinnedLRUHandle*> last_reference_list;
 
@@ -354,6 +350,7 @@ rocksdb::Status BinnedLRUCacheShard::Insert(const rocksdb::Slice& key, uint32_t
   e->deleter = deleter;
   e->charge = charge;
   e->key_length = key.size();
+  e->key_data = new char[e->key_length];
   e->flags = 0;
   e->hash = hash;
   e->refs = (handle == nullptr
@@ -362,7 +359,7 @@ rocksdb::Status BinnedLRUCacheShard::Insert(const rocksdb::Slice& key, uint32_t
   e->next = e->prev = nullptr;
   e->SetInCache(true);
   e->SetPriority(priority);
-  memcpy(e->key_data, key.data(), key.size());
+  std::copy_n(key.data(), e->key_length, e->key_data);
 
   {
     std::lock_guard<std::mutex> l(mutex_);
@@ -377,7 +374,7 @@ rocksdb::Status BinnedLRUCacheShard::Insert(const rocksdb::Slice& key, uint32_t
         // into cache and get evicted immediately.
         last_reference_list.push_back(e);
       } else {
-        delete[] reinterpret_cast<char*>(e);
+        delete e;
         *handle = nullptr;
         s = rocksdb::Status::Incomplete("Insert failed due to LRU cache being full.");
       }
index 17f91723037f0d6387960fcb4d5362ff15407db1..d48286a1dab265519e91783864b0e75092f59eb4 100644 (file)
@@ -71,7 +71,7 @@ struct BinnedLRUHandle {
 
   uint32_t hash;     // Hash of key(); used for fast sharding and comparisons
 
-  char key_data[1];  // Beginning of key
+  char* key_data = nullptr;  // Beginning of key
 
   rocksdb::Slice key() const {
     // For cheaper lookups, we allow a temporary Handle object
@@ -119,7 +119,8 @@ struct BinnedLRUHandle {
     if (deleter) {
       (*deleter)(key(), value);
     }
-    delete[] reinterpret_cast<char*>(this);
+    delete[] key_data;
+    delete this;
   }
 };