diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index ad547cf543e..ec2ba0bffaf 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -23,6 +23,7 @@ #include "checkcondition.h" #include "astutils.h" +#include "errortypes.h" #include "library.h" #include "platform.h" #include "settings.h" @@ -1112,8 +1113,18 @@ static bool parseComparison(const Token *comp, bool ¬1, std::string &op, std: inconclusive = inconclusive || (value[0] == '\'' && !(op == "!=" || op == "==")); + if (value[0] == '\'') { + // Bad character literal + try { + (void)MathLib::toBigNumber(value, expr); + } catch (const InternalError & /*e*/) { + return false; + } + return true; + } + // Only float and int values are currently handled - return MathLib::isInt(value) || MathLib::isFloat(value) || (value[0] == '\''); + return MathLib::isInt(value) || MathLib::isFloat(value); } static std::string conditionString(bool not1, const Token *expr1, const std::string &op, const std::string &value1) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index d28b350ea8b..d162c363955 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -2766,7 +2766,11 @@ bool TemplateSimplifier::simplifyCalculations(Token* frontToken, const Token *ba if (validTokenEnd(bounded, tok, backToken, 3) && Token::Match(tok->previous(), "(|&&|%oror% %char% %comp% %num% &&|%oror%|)")) { - tok->str(MathLib::toString(MathLib::toBigNumber(tok))); + try { + tok->str(MathLib::toString(MathLib::toBigNumber(tok))); + } catch (const InternalError & /*e*/) { + // Bad character literal + } } if (validTokenEnd(bounded, tok, backToken, 5) && diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 72873ac2475..0b4380131c1 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4192,8 +4192,13 @@ void Tokenizer::simplifyCaseRange() { for (Token* tok = list.front(); tok; tok = tok->next()) { if (Token::Match(tok, "case %num%|%char% ... %num%|%char% :")) { - const MathLib::bigint start = MathLib::toBigNumber(tok->tokAt(1)); - MathLib::bigint end = MathLib::toBigNumber(tok->tokAt(3)); + MathLib::bigint start, end; + try { + start = MathLib::toBigNumber(tok->tokAt(1)); + end = MathLib::toBigNumber(tok->tokAt(3)); + } catch (const InternalError & /*e*/) { + continue; // Bad character literal + } end = std::min(start + 50, end); // Simplify it 50 times at maximum if (start < end) { tok = tok->tokAt(2); diff --git a/lib/vf_common.cpp b/lib/vf_common.cpp index a96c0ea9ad4..65fa31ec1b3 100644 --- a/lib/vf_common.cpp +++ b/lib/vf_common.cpp @@ -19,6 +19,7 @@ #include "vf_common.h" #include "astutils.h" +#include "errortypes.h" #include "library.h" #include "mathlib.h" #include "path.h" @@ -137,6 +138,8 @@ namespace ValueFlow setTokenValue(tok, std::move(value), settings); } catch (const std::exception & /*e*/) { // Bad character literal + } catch (const InternalError & /*e*/) { + // Bad character literal } } else if (tok->isNumber() && MathLib::isFloat(tok->str())) { // A long double literal out of the range of double (1e400L) has no @@ -323,6 +326,9 @@ namespace ValueFlow catch (const std::exception& /*e*/) { // Bad integer literal } + catch (const InternalError& /*e*/) { + // Bad character literal + } } sz = 0; break; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 5961dca21a9..cb0561690e8 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -70,6 +70,7 @@ class TestCondition : public TestFixture { TEST_CASE(incorrectLogicOperator16); // #10070 TEST_CASE(incorrectLogicOperator17); TEST_CASE(incorrectLogicOperator18); // #12282 + TEST_CASE(incorrectLogicOperatorBadCharLiteral); TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError); TEST_CASE(incorrectLogicOp_condSwapping); TEST_CASE(testBug5895); @@ -1785,6 +1786,13 @@ class TestCondition : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void incorrectLogicOperatorBadCharLiteral() { + check("void f(char c) {\n" + " if (c == '\\s' && c == '\\q') {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + } + void incorrectLogicOperator17() { // #12471 check("struct R {\n" " void set() { i = 1; }\n" diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 407649e0c7f..53d752e0853 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -288,6 +288,7 @@ class TestSimplifyTemplate : public TestFixture { TEST_CASE(simplifyTemplateArgs1); TEST_CASE(simplifyTemplateArgs2); TEST_CASE(simplifyTemplateArgs3); + TEST_CASE(simplifyTemplateArgsInvalidCharLiteral); TEST_CASE(template_variadic_1); // #9144 TEST_CASE(template_variadic_2); // #4349 @@ -6368,6 +6369,12 @@ class TestSimplifyTemplate : public TestFixture { ASSERT_EQUALS("foo = false ; foo ;", tok("template foo = N; foo < ( 1 - 1) ? true : false >;\n")); } + void simplifyTemplateArgsInvalidCharLiteral() { + ASSERT_EQUALS("struct V<('\\s'==1)> ; V<('\\s'==1)> v ; struct V<('\\s'==1)> { } ;", + tok("template struct V {};\n" + "V<('\\s' == 1)> v;\n")); + } + void simplifyTemplateArgs2() { const char code[] = "template struct a_t { static const bool t = T; };\n" "typedef a_t a;\n" diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7a8d2512b4f..189ba174892 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -547,6 +547,8 @@ class TestTokenizer : public TestFixture { TEST_CASE(simplifyEnum1); TEST_CASE(simplifyEnum2); + + TEST_CASE(nonStandardCharLiteral); } class TokenizerTest final : public Tokenizer @@ -9547,6 +9549,18 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS(tok->column(), 24); ASSERT_EQUALS(tok->next()->column(), 16); } + + void nonStandardCharLiteral() { + const Settings s = settingsBuilder(settings2).severity(Severity::portability).build(); + (void)tokenizeAndStringify("int a = '\\s';\n", s, false); + ASSERT_EQUALS("[test.c:1:9]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str()); + + (void)tokenizeAndStringify("void f(char c) { switch (c) { case '\\s' ... 'z': break; } }\n", s, false); + ASSERT_EQUALS("[test.c:1:36]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str()); + + (void)tokenizeAndStringify("int n = sizeof(char['\\s']);\n", s, false); + ASSERT_EQUALS("[test.c:1:21]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str()); + } }; REGISTER_TEST(TestTokenizer)