Skip to content

Commit c10a3ee

Browse files
authored
Fix #15298: Internal error with invalid escape sequence. (#9104)
1 parent 4ffe325 commit c10a3ee

7 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎lib/checkcondition.cpp‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "checkcondition.h"
2424

2525
#include "astutils.h"
26+
#include "errortypes.h"
2627
#include "library.h"
2728
#include "platform.h"
2829
#include "settings.h"
@@ -1112,8 +1113,18 @@ static bool parseComparison(const Token *comp, bool &not1, std::string &op, std:
11121113

11131114
inconclusive = inconclusive || (value[0] == '\'' && !(op == "!=" || op == "=="));
11141115

1116+
if (value[0] == '\'') {
1117+
// Bad character literal
1118+
try {
1119+
(void)MathLib::toBigNumber(value, expr);
1120+
} catch (const InternalError & /*e*/) {
1121+
return false;
1122+
}
1123+
return true;
1124+
}
1125+
11151126
// Only float and int values are currently handled
1116-
return MathLib::isInt(value) || MathLib::isFloat(value) || (value[0] == '\'');
1127+
return MathLib::isInt(value) || MathLib::isFloat(value);
11171128
}
11181129

11191130
static std::string conditionString(bool not1, const Token *expr1, const std::string &op, const std::string &value1)

‎lib/templatesimplifier.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2766,7 +2766,11 @@ bool TemplateSimplifier::simplifyCalculations(Token* frontToken, const Token *ba
27662766

27672767
if (validTokenEnd(bounded, tok, backToken, 3) &&
27682768
Token::Match(tok->previous(), "(|&&|%oror% %char% %comp% %num% &&|%oror%|)")) {
2769-
tok->str(MathLib::toString(MathLib::toBigNumber(tok)));
2769+
try {
2770+
tok->str(MathLib::toString(MathLib::toBigNumber(tok)));
2771+
} catch (const InternalError & /*e*/) {
2772+
// Bad character literal
2773+
}
27702774
}
27712775

27722776
if (validTokenEnd(bounded, tok, backToken, 5) &&

‎lib/tokenize.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4214,8 +4214,13 @@ void Tokenizer::simplifyCaseRange()
42144214
{
42154215
for (Token* tok = list.front(); tok; tok = tok->next()) {
42164216
if (Token::Match(tok, "case %num%|%char% ... %num%|%char% :")) {
4217-
const MathLib::bigint start = MathLib::toBigNumber(tok->tokAt(1));
4218-
MathLib::bigint end = MathLib::toBigNumber(tok->tokAt(3));
4217+
MathLib::bigint start, end;
4218+
try {
4219+
start = MathLib::toBigNumber(tok->tokAt(1));
4220+
end = MathLib::toBigNumber(tok->tokAt(3));
4221+
} catch (const InternalError & /*e*/) {
4222+
continue; // Bad character literal
4223+
}
42194224
end = std::min(start + 50, end); // Simplify it 50 times at maximum
42204225
if (start < end) {
42214226
tok = tok->tokAt(2);

‎lib/vf_common.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "vf_common.h"
2020

2121
#include "astutils.h"
22+
#include "errortypes.h"
2223
#include "library.h"
2324
#include "mathlib.h"
2425
#include "path.h"
@@ -137,6 +138,8 @@ namespace ValueFlow
137138
setTokenValue(tok, std::move(value), settings);
138139
} catch (const std::exception & /*e*/) {
139140
// Bad character literal
141+
} catch (const InternalError & /*e*/) {
142+
// Bad character literal
140143
}
141144
} else if (tok->isNumber() && MathLib::isFloat(tok->str())) {
142145
// A long double literal out of the range of double (1e400L) has no
@@ -323,6 +326,9 @@ namespace ValueFlow
323326
catch (const std::exception& /*e*/) {
324327
// Bad integer literal
325328
}
329+
catch (const InternalError& /*e*/) {
330+
// Bad character literal
331+
}
326332
}
327333
sz = 0;
328334
break;

‎test/testcondition.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class TestCondition : public TestFixture {
7070
TEST_CASE(incorrectLogicOperator16); // #10070
7171
TEST_CASE(incorrectLogicOperator17);
7272
TEST_CASE(incorrectLogicOperator18); // #12282
73+
TEST_CASE(incorrectLogicOperatorBadCharLiteral);
7374
TEST_CASE(secondAlwaysTrueFalseWhenFirstTrueError);
7475
TEST_CASE(incorrectLogicOp_condSwapping);
7576
TEST_CASE(testBug5895);
@@ -1785,6 +1786,13 @@ class TestCondition : public TestFixture {
17851786
ASSERT_EQUALS("", errout_str());
17861787
}
17871788

1789+
void incorrectLogicOperatorBadCharLiteral() {
1790+
check("void f(char c) {\n"
1791+
" if (c == '\\s' && c == '\\q') {}\n"
1792+
"}\n");
1793+
ASSERT_EQUALS("", errout_str());
1794+
}
1795+
17881796
void incorrectLogicOperator17() { // #12471
17891797
check("struct R {\n"
17901798
" void set() { i = 1; }\n"

‎test/testsimplifytemplate.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ class TestSimplifyTemplate : public TestFixture {
288288
TEST_CASE(simplifyTemplateArgs1);
289289
TEST_CASE(simplifyTemplateArgs2);
290290
TEST_CASE(simplifyTemplateArgs3);
291+
TEST_CASE(simplifyTemplateArgsInvalidCharLiteral);
291292

292293
TEST_CASE(template_variadic_1); // #9144
293294
TEST_CASE(template_variadic_2); // #4349
@@ -6368,6 +6369,12 @@ class TestSimplifyTemplate : public TestFixture {
63686369
ASSERT_EQUALS("foo<false> = false ; foo<false> ;", tok("template<bool N> foo = N; foo < ( 1 - 1) ? true : false >;\n"));
63696370
}
63706371

6372+
void simplifyTemplateArgsInvalidCharLiteral() {
6373+
ASSERT_EQUALS("struct V<('\\s'==1)> ; V<('\\s'==1)> v ; struct V<('\\s'==1)> { } ;",
6374+
tok("template<bool B> struct V {};\n"
6375+
"V<('\\s' == 1)> v;\n"));
6376+
}
6377+
63716378
void simplifyTemplateArgs2() {
63726379
const char code[] = "template<bool T> struct a_t { static const bool t = T; };\n"
63736380
"typedef a_t<sizeof(void*) == sizeof(char)> a;\n"

‎test/testtokenize.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ class TestTokenizer : public TestFixture {
548548
TEST_CASE(simplifyEnum1);
549549

550550
TEST_CASE(simplifyEnum2);
551+
552+
TEST_CASE(nonStandardCharLiteral);
551553
}
552554

553555
class TokenizerTest final : public Tokenizer
@@ -9554,6 +9556,18 @@ class TestTokenizer : public TestFixture {
95549556
ASSERT_EQUALS(tok->column(), 24);
95559557
ASSERT_EQUALS(tok->next()->column(), 16);
95569558
}
9559+
9560+
void nonStandardCharLiteral() {
9561+
const Settings s = settingsBuilder(settings2).severity(Severity::portability).build();
9562+
(void)tokenizeAndStringify("int a = '\\s';\n", s, false);
9563+
ASSERT_EQUALS("[test.c:1:9]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str());
9564+
9565+
(void)tokenizeAndStringify("void f(char c) { switch (c) { case '\\s' ... 'z': break; } }\n", s, false);
9566+
ASSERT_EQUALS("[test.c:1:36]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str());
9567+
9568+
(void)tokenizeAndStringify("int n = sizeof(char['\\s']);\n", s, false);
9569+
ASSERT_EQUALS("[test.c:1:21]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str());
9570+
}
95579571
};
95589572

95599573
REGISTER_TEST(TestTokenizer)

0 commit comments

Comments
 (0)