From a7b5c5b5841eaed39e0ca918a75877da6f0404b6 Mon Sep 17 00:00:00 2001 From: Diego Novillo Date: Fri, 1 May 2015 17:59:15 +0000 Subject: [PATCH] Fix infinite recursion in ScaledNumber::toInt. Patch from dexonsmith. The call to toInt() was calling compareTo() which in some cases would call back to toInt(), creating an infinite loop. Fixed by simplifying the logic in compareTo() to avoid the co-recursion. llvm-svn: 236326 --- llvm/include/llvm/Support/ScaledNumber.h | 9 +-------- llvm/unittests/Support/ScaledNumberTest.cpp | 5 +++++ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/llvm/include/llvm/Support/ScaledNumber.h b/llvm/include/llvm/Support/ScaledNumber.h index dbe88f2..0a4262b 100644 --- a/llvm/include/llvm/Support/ScaledNumber.h +++ b/llvm/include/llvm/Support/ScaledNumber.h @@ -670,14 +670,7 @@ public: return ScaledNumbers::compare(Digits, Scale, X.Digits, X.Scale); } int compareTo(uint64_t N) const { - ScaledNumber Scaled = get(N); - int Compare = compare(Scaled); - if (Width == 64 || Compare != 0) - return Compare; - - // Check for precision loss. We know *this == RoundTrip. - uint64_t RoundTrip = Scaled.template toInt(); - return N == RoundTrip ? 0 : RoundTrip < N ? -1 : 1; + return ScaledNumbers::compare(Digits, Scale, N, 0); } int compareTo(int64_t N) const { return N < 0 ? 1 : compareTo(uint64_t(N)); } diff --git a/llvm/unittests/Support/ScaledNumberTest.cpp b/llvm/unittests/Support/ScaledNumberTest.cpp index 3872155..2f38b2a 100644 --- a/llvm/unittests/Support/ScaledNumberTest.cpp +++ b/llvm/unittests/Support/ScaledNumberTest.cpp @@ -556,4 +556,9 @@ TEST(ScaledNumberHelpersTest, arithmeticOperators) { EXPECT_EQ(ScaledNumber(1, 4), ScaledNumber(1, 3) << 1); } +TEST(ScaledNumberHelpersTest, toIntBug) { + ScaledNumber n(1, 0); + EXPECT_EQ(1u, (n * n).toInt()); +} + } // end namespace -- 2.7.4