diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 68a3cae82f6..c23182a5a63 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -344,8 +344,13 @@ MathLib::biguint MathLib::toBigUNumber(const std::string & str, const Token * co return static_cast(static_cast(doubleval)); } - if (isCharLiteral(str)) - return simplecpp::characterLiteralToLL(str); + if (isCharLiteral(str)) { + try { + return simplecpp::characterLiteralToLL(str); + } catch (const std::runtime_error& e) { + throw InternalError(tok, "Internal Error. MathLib::toBigUNumber: characterLiteralToLL(" + str + ") => " + e.what()); + } + } try { std::size_t idx = 0; @@ -429,8 +434,13 @@ MathLib::bigint MathLib::toBigNumber(const std::string & str, const Token * cons return static_cast(doubleval); } - if (isCharLiteral(str)) - return simplecpp::characterLiteralToLL(str); + if (isCharLiteral(str)) { + try { + return simplecpp::characterLiteralToLL(str); + } catch (const std::runtime_error& e) { + throw InternalError(tok, "Internal Error. MathLib::toBigNumber: characterLiteralToLL(" + str + ") => " + e.what()); + } + } try { std::size_t idx = 0; @@ -505,7 +515,7 @@ double MathLib::toDoubleNumber(const std::string &str, const Token * const tok) if (isCharLiteral(str)) { try { return simplecpp::characterLiteralToLL(str); - } catch (const std::exception& e) { + } catch (const std::runtime_error& e) { throw InternalError(tok, "Internal Error. MathLib::toDoubleNumber: characterLiteralToLL(" + str + ") => " + e.what()); } } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index aaed296dc4c..0763207fe92 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3470,7 +3470,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration, int fileIndex) if (tok->tokType() == Token::eChar && tok->values().empty()) { try { simplecpp::characterLiteralToLL(tok->str()); - } catch (const std::exception &e) { + } catch (const std::runtime_error &e) { unhandledCharLiteral(tok, e.what()); } } diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 6a0ba4c0871..a2f9526162c 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -416,6 +416,8 @@ class TestMathLib : public TestFixture { TokenList::deleteTokens(tok); } + ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigNumber("''"), INTERNAL, "Internal Error. MathLib::toBigNumber: characterLiteralToLL('') => empty character literal"); + // TODO: test binary // TODO: test floating point @@ -588,6 +590,8 @@ class TestMathLib : public TestFixture { TokenList::deleteTokens(tok); } + ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigUNumber("''"), INTERNAL, "Internal Error. MathLib::toBigUNumber: characterLiteralToLL('') => empty character literal"); + // TODO: test binary // TODO: test floating point @@ -736,6 +740,8 @@ class TestMathLib : public TestFixture { ASSERT_EQUALS("0.0", MathLib::toString(MathLib::toDoubleNumber("-0"))); ASSERT_EQUALS("0.0", MathLib::toString(MathLib::toDoubleNumber("-0."))); ASSERT_EQUALS("0.0", MathLib::toString(MathLib::toDoubleNumber("-0.0"))); + + ASSERT_THROW_INTERNAL_EQUALS(MathLib::toDoubleNumber("''"), INTERNAL, "Internal Error. MathLib::toDoubleNumber: characterLiteralToLL('') => empty character literal"); } void isint() const {