diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2016-04-01 15:51:51 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2016-04-01 15:51:51 +0000 |
commit | 99c67b31cb34f1821e5037bc79598306733a906a (patch) | |
tree | 713b42c305ecaaf7a320c8672e5247db4b95b970 /llvm/include | |
parent | 7784171721b7bc85732c302ab4c6fe2122b9a48d (diff) | |
download | bcm5719-llvm-99c67b31cb34f1821e5037bc79598306733a906a.tar.gz bcm5719-llvm-99c67b31cb34f1821e5037bc79598306733a906a.zip |
[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.
llvm-svn: 265145
Diffstat (limited to 'llvm/include')
-rw-r--r-- | llvm/include/llvm/ADT/StringMap.h | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/llvm/include/llvm/ADT/StringMap.h b/llvm/include/llvm/ADT/StringMap.h index ce80484738f..e593e5165bd 100644 --- a/llvm/include/llvm/ADT/StringMap.h +++ b/llvm/include/llvm/ADT/StringMap.h @@ -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]; } |