[llvm] r199609 - [APInt] Fixed bug where APInt(UINT32_MAX, 0) would blow up when being constructed.

Michael Gottesman mgottesman at apple.com
Sun Jan 19 12:33:38 PST 2014


Author: mgottesman
Date: Sun Jan 19 14:33:38 2014
New Revision: 199609

URL: http://llvm.org/viewvc/llvm-project?rev=199609&view=rev
Log:
[APInt] Fixed bug where APInt(UINT32_MAX, 0) would blow up when being constructed.

This was due to arithmetic overflow in the getNumBits() computation. Now we
cast BitWidth to a uint64_t so that does not occur during the computation. After
the computation is complete, the uint64_t is truncated when the function
returns.

I know that this is not something that is likely to happen, but it *IS* a valid
input and we should not blow up.

Modified:
    llvm/trunk/include/llvm/ADT/APInt.h
    llvm/trunk/unittests/ADT/APIntTest.cpp

Modified: llvm/trunk/include/llvm/ADT/APInt.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=199609&r1=199608&r2=199609&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APInt.h Sun Jan 19 14:33:38 2014
@@ -1265,7 +1265,7 @@ public:
   /// \returns the number of words to hold the integer value with a given bit
   /// width.
   static unsigned getNumWords(unsigned BitWidth) {
-    return (BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
+    return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
   }
 
   /// \brief Compute the number of active bits in the value

Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=199609&r1=199608&r2=199609&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Sun Jan 19 14:33:38 2014
@@ -623,6 +623,14 @@ TEST(APIntTest, arrayAccess) {
   }
 }
 
+TEST(APIntTest, LargeAPIntConstruction) {
+  // Check that we can properly construct very large APInt. It is very
+  // unlikely that people will ever do this, but it is a legal input,
+  // so we should not crash on it.
+  APInt A9(UINT32_MAX, 0);
+  EXPECT_FALSE(A9.getBoolValue());
+}
+
 TEST(APIntTest, nearestLogBase2) {
   // Single word check.  
 





More information about the llvm-commits mailing list