From: Chris Lattner Date: Sat, 25 Apr 2009 18:34:04 +0000 (+0000) Subject: Fix PR4040: APInt's string constructor is too strict X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=38300e91f5ff2d427d98f81fb25df8cc2800d985;p=oota-llvm.git Fix PR4040: APInt's string constructor is too strict patch by Jeff Yasskin! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70058 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index 1cabe0f03e7..8ac589c865f 100644 --- a/lib/Support/APInt.cpp +++ b/lib/Support/APInt.cpp @@ -1918,9 +1918,9 @@ void APInt::fromString(unsigned numbits, const char *str, unsigned slen, if (isNeg) str++, slen--; assert((slen <= numbits || radix != 2) && "Insufficient bit width"); - assert((slen*3 <= numbits || radix != 8) && "Insufficient bit width"); - assert((slen*4 <= numbits || radix != 16) && "Insufficient bit width"); - assert(((slen*64)/22 <= numbits || radix != 10) && "Insufficient bit width"); + assert(((slen-1)*3 <= numbits || radix != 8) && "Insufficient bit width"); + assert(((slen-1)*4 <= numbits || radix != 16) && "Insufficient bit width"); + assert((((slen-1)*64)/22 <= numbits || radix != 10) && "Insufficient bit width"); // Allocate memory if (!isSingleWord()) @@ -1961,10 +1961,12 @@ void APInt::fromString(unsigned numbits, const char *str, unsigned slen, } // Shift or multiply the value by the radix - if (shift) - *this <<= shift; - else - *this *= apradix; + if (slen > 1) { + if (shift) + *this <<= shift; + else + *this *= apradix; + } // Add in the digit we just interpreted if (apdigit.isSingleWord()) diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp index dcafae2fee8..8ff7d1d8099 100644 --- a/unittests/ADT/APIntTest.cpp +++ b/unittests/ADT/APIntTest.cpp @@ -176,4 +176,13 @@ TEST(APIntTest, i1) { EXPECT_EQ(zero, one.srem(neg_one)); } +TEST(APIntTest, fromString) { + EXPECT_EQ(APInt(1, 0), APInt(1, "0", 1, 10)); + EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 10)); + EXPECT_EQ(APInt(1, 1), APInt(1, "-1", 2, 10)); + EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 2)); + EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 8)); + EXPECT_EQ(APInt(1, 1), APInt(1, "1", 1, 16)); +} + }