[llvm] r204010 - PointerIntPair: Avoid an (academic) case of undefined behavior in the DenseMapInfo specialization.

Benjamin Kramer benny.kra at googlemail.com
Sat Mar 15 11:10:49 PDT 2014


Author: d0k
Date: Sat Mar 15 13:10:49 2014
New Revision: 204010

URL: http://llvm.org/viewvc/llvm-project?rev=204010&view=rev
Log:
PointerIntPair: Avoid an (academic) case of undefined behavior in the DenseMapInfo specialization.

If we use a pair with an enum type this could create values outside
of the enum range. Avoid it by creating the bit pattern directly.
While there turn a dynamic assert into a static one. No functionality
change.

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

Modified: llvm/trunk/include/llvm/ADT/PointerIntPair.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/PointerIntPair.h?rev=204010&r1=204009&r2=204010&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/PointerIntPair.h (original)
+++ llvm/trunk/include/llvm/ADT/PointerIntPair.h Sat Mar 15 13:10:49 2014
@@ -45,6 +45,8 @@ class PointerIntPair {
   static_assert(PtrTraits::NumLowBitsAvailable <
                 std::numeric_limits<uintptr_t>::digits,
                 "cannot use a pointer type that has all bits free");
+  static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
+                "PointerIntPair with integer size too large for pointer");
   enum : uintptr_t {
     /// PointerBitMask - The bits that come from the pointer.
     PointerBitMask =
@@ -63,8 +65,6 @@ class PointerIntPair {
 public:
   PointerIntPair() : Value(0) {}
   PointerIntPair(PointerTy PtrVal, IntType IntVal) {
-    assert(IntBits <= PtrTraits::NumLowBitsAvailable &&
-           "PointerIntPair formed with integer size too large for pointer");
     setPointerAndInt(PtrVal, IntVal);
   }
   explicit PointerIntPair(PointerTy PtrVal) {
@@ -162,13 +162,13 @@ struct DenseMapInfo<PointerIntPair<Point
   typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
   static Ty getEmptyKey() {
     uintptr_t Val = static_cast<uintptr_t>(-1);
-    Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
-    return Ty(reinterpret_cast<PointerTy>(Val), IntType((1 << IntBits)-1));
+    Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
+    return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
   }
   static Ty getTombstoneKey() {
     uintptr_t Val = static_cast<uintptr_t>(-2);
     Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
-    return Ty(reinterpret_cast<PointerTy>(Val), IntType(0));
+    return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
   }
   static unsigned getHashValue(Ty V) {
     uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());





More information about the llvm-commits mailing list