Skip to content

Commit cf76958

Browse files
authored
fixed missing exception handling in MathLib::toBig{U}Number() (#8025)
1 parent f934fa3 commit cf76958

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

lib/mathlib.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,13 @@ MathLib::biguint MathLib::toBigUNumber(const std::string & str, const Token * co
344344
return static_cast<biguint>(static_cast<bigint>(doubleval));
345345
}
346346

347-
if (isCharLiteral(str))
348-
return simplecpp::characterLiteralToLL(str);
347+
if (isCharLiteral(str)) {
348+
try {
349+
return simplecpp::characterLiteralToLL(str);
350+
} catch (const std::runtime_error& e) {
351+
throw InternalError(tok, "Internal Error. MathLib::toBigUNumber: characterLiteralToLL(" + str + ") => " + e.what());
352+
}
353+
}
349354

350355
try {
351356
std::size_t idx = 0;
@@ -429,8 +434,13 @@ MathLib::bigint MathLib::toBigNumber(const std::string & str, const Token * cons
429434
return static_cast<bigint>(doubleval);
430435
}
431436

432-
if (isCharLiteral(str))
433-
return simplecpp::characterLiteralToLL(str);
437+
if (isCharLiteral(str)) {
438+
try {
439+
return simplecpp::characterLiteralToLL(str);
440+
} catch (const std::runtime_error& e) {
441+
throw InternalError(tok, "Internal Error. MathLib::toBigNumber: characterLiteralToLL(" + str + ") => " + e.what());
442+
}
443+
}
434444

435445
try {
436446
std::size_t idx = 0;
@@ -505,7 +515,7 @@ double MathLib::toDoubleNumber(const std::string &str, const Token * const tok)
505515
if (isCharLiteral(str)) {
506516
try {
507517
return simplecpp::characterLiteralToLL(str);
508-
} catch (const std::exception& e) {
518+
} catch (const std::runtime_error& e) {
509519
throw InternalError(tok, "Internal Error. MathLib::toDoubleNumber: characterLiteralToLL(" + str + ") => " + e.what());
510520
}
511521
}

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3470,7 +3470,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration, int fileIndex)
34703470
if (tok->tokType() == Token::eChar && tok->values().empty()) {
34713471
try {
34723472
simplecpp::characterLiteralToLL(tok->str());
3473-
} catch (const std::exception &e) {
3473+
} catch (const std::runtime_error &e) {
34743474
unhandledCharLiteral(tok, e.what());
34753475
}
34763476
}

test/testmathlib.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ class TestMathLib : public TestFixture {
416416
TokenList::deleteTokens(tok);
417417
}
418418

419+
ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigNumber("''"), INTERNAL, "Internal Error. MathLib::toBigNumber: characterLiteralToLL('') => empty character literal");
420+
419421
// TODO: test binary
420422
// TODO: test floating point
421423

@@ -588,6 +590,8 @@ class TestMathLib : public TestFixture {
588590
TokenList::deleteTokens(tok);
589591
}
590592

593+
ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigUNumber("''"), INTERNAL, "Internal Error. MathLib::toBigUNumber: characterLiteralToLL('') => empty character literal");
594+
591595
// TODO: test binary
592596
// TODO: test floating point
593597

@@ -736,6 +740,8 @@ class TestMathLib : public TestFixture {
736740
ASSERT_EQUALS("0.0", MathLib::toString(MathLib::toDoubleNumber("-0")));
737741
ASSERT_EQUALS("0.0", MathLib::toString(MathLib::toDoubleNumber("-0.")));
738742
ASSERT_EQUALS("0.0", MathLib::toString(MathLib::toDoubleNumber("-0.0")));
743+
744+
ASSERT_THROW_INTERNAL_EQUALS(MathLib::toDoubleNumber("''"), INTERNAL, "Internal Error. MathLib::toDoubleNumber: characterLiteralToLL('') => empty character literal");
739745
}
740746

741747
void isint() const {

0 commit comments

Comments
 (0)