[llvm] r265145 - [ADT] Make StringMap's tombstone aligned.

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 1 08:51:51 PDT 2016


Author: d0k
Date: Fri Apr  1 10:51:51 2016
New Revision: 265145

URL: http://llvm.org/viewvc/llvm-project?rev=265145&view=rev
Log:
[ADT] Make StringMap's tombstone aligned.

This avoids undefined behavior when casting pointers to it. Also make
sure that we don't cast to a derived StringMapEntry before checking for
tombstone, as that may have different alignment requirements.

Modified:
    llvm/trunk/include/llvm/ADT/StringMap.h

Modified: llvm/trunk/include/llvm/ADT/StringMap.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringMap.h?rev=265145&r1=265144&r2=265145&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringMap.h (original)
+++ llvm/trunk/include/llvm/ADT/StringMap.h Fri Apr  1 10:51:51 2016
@@ -17,6 +17,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/Allocator.h"
+#include "llvm/Support/PointerLikeTypeTraits.h"
 #include <cstring>
 #include <utility>
 
@@ -95,7 +96,9 @@ protected:
 
 public:
   static StringMapEntryBase *getTombstoneVal() {
-    return (StringMapEntryBase*)-1;
+    uintptr_t Val = static_cast<uintptr_t>(-1);
+    Val <<= PointerLikeTypeTraits<StringMapEntryBase *>::NumLowBitsAvailable;
+    return reinterpret_cast<StringMapEntryBase *>(Val);
   }
 
   unsigned getNumBuckets() const { return NumBuckets; }
@@ -260,14 +263,15 @@ public:
     NumItems = RHS.NumItems;
     NumTombstones = RHS.NumTombstones;
     for (unsigned I = 0, E = NumBuckets; I != E; ++I) {
-      MapEntryTy *Bucket = ((MapEntryTy**) RHS.TheTable)[I];
+      StringMapEntryBase *Bucket = RHS.TheTable[I];
       if (!Bucket || Bucket == getTombstoneVal()) {
         TheTable[I] = Bucket;
         continue;
       }
 
-      TheTable[I] = MapEntryTy::Create(Bucket->getKey(), Allocator,
-                                       Bucket->getValue());
+      TheTable[I] = MapEntryTy::Create(
+          static_cast<MapEntryTy *>(Bucket)->getKey(), Allocator,
+          static_cast<MapEntryTy *>(Bucket)->getValue());
       HashTable[I] = RHSHashTable[I];
     }
 




More information about the llvm-commits mailing list