From c6e74f4c101fe30a946e6fe8ee1f8799bfdfd4ea Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Sun, 12 Jan 2020 08:11:12 +0100 Subject: [PATCH] Mathlib: Fix value of suffixed binary integer literal (#2487) --- lib/mathlib.cpp | 4 ++++ test/testmathlib.cpp | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index df9bbd388..af7d8604d 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -319,6 +319,8 @@ MathLib::biguint MathLib::toULongNumber(const std::string & str) if (isBin(str)) { biguint ret = 0; for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) { + if (str[i] != '1' && str[i] != '0') + break; ret <<= 1; if (str[i] == '1') ret |= 1; @@ -510,6 +512,8 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str) if (isBin(str)) { bigint ret = 0; for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) { + if (str[i] != '1' && str[i] != '0') + break; ret <<= 1; if (str[i] == '1') ret |= 1; diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index f31902114..f17cf71b1 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -272,6 +272,11 @@ private: // from binary ASSERT_EQUALS(0, MathLib::toLongNumber("0b0")); ASSERT_EQUALS(1, MathLib::toLongNumber("0b1")); + ASSERT_EQUALS(1, MathLib::toLongNumber("0b1U")); + ASSERT_EQUALS(1, MathLib::toLongNumber("0b1L")); + ASSERT_EQUALS(1, MathLib::toLongNumber("0b1LU")); + ASSERT_EQUALS(1, MathLib::toLongNumber("0b1LL")); + ASSERT_EQUALS(1, MathLib::toLongNumber("0b1LLU")); ASSERT_EQUALS(1, MathLib::toLongNumber("+0b1")); ASSERT_EQUALS(-1, MathLib::toLongNumber("-0b1")); ASSERT_EQUALS(215, MathLib::toLongNumber("0b11010111")); @@ -347,6 +352,13 @@ private: ASSERT_EQUALS(10000U, MathLib::toULongNumber("1e4")); ASSERT_EQUALS(0xFF00000000000000UL, MathLib::toULongNumber("0xFF00000000000000UL")); ASSERT_EQUALS(0x0A00000000000000UL, MathLib::toULongNumber("0x0A00000000000000UL")); + ASSERT_EQUALS(0, MathLib::toULongNumber("0b0")); + ASSERT_EQUALS(1, MathLib::toULongNumber("0b1")); + ASSERT_EQUALS(1, MathLib::toULongNumber("0b1U")); + ASSERT_EQUALS(1, MathLib::toULongNumber("0b1L")); + ASSERT_EQUALS(1, MathLib::toULongNumber("0b1LU")); + ASSERT_EQUALS(1, MathLib::toULongNumber("0b1LL")); + ASSERT_EQUALS(1, MathLib::toULongNumber("0b1LLU")); ASSERT_EQUALS(9U, MathLib::toULongNumber("011")); ASSERT_EQUALS(5U, MathLib::toULongNumber("0b101"));