Skip to content

Commit 356b2bd

Browse files
Fix fuzzing crash: reject semicolon within parentheses (#6197)
Poached from #6116
1 parent 150aacf commit 356b2bd

6 files changed

Lines changed: 23 additions & 8 deletions

File tree

lib/tokenize.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8646,6 +8646,20 @@ void Tokenizer::findGarbageCode() const
86468646
syntaxError(tok);
86478647
if (Token::Match(tok, "& %comp%|&&|%oror%|&|%or%") && tok->strAt(1) != ">")
86488648
syntaxError(tok);
8649+
8650+
if (tok->link() && Token::Match(tok, "[([]") && (!tok->tokAt(-1) || !tok->tokAt(-1)->isControlFlowKeyword())) {
8651+
const Token* const end = tok->link();
8652+
for (const Token* inner = tok->next(); inner != end; inner = inner->next()) {
8653+
if (inner->str() == "{")
8654+
inner = inner->link();
8655+
else if (inner->str() == ";") {
8656+
if (tok->tokAt(-1) && tok->tokAt(-1)->isUpperCaseName())
8657+
unknownMacroError(tok->tokAt(-1));
8658+
else
8659+
syntaxError(inner);
8660+
}
8661+
}
8662+
}
86498663
}
86508664

86518665
// ternary operator without :
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
i a;u n(;a[]),n(){a[]=0}

test/testgarbage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ class TestGarbage : public TestFixture {
460460
}
461461

462462
void garbageCode5() { // #5168
463-
checkCode("( asm : ; void : );");
463+
ASSERT_THROW(checkCode("( asm : ; void : );"), InternalError);
464464
}
465465

466466
void garbageCode6() { // #5214

test/testsimplifytypedef.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,8 +2511,7 @@ class TestSimplifyTypedef : public TestFixture {
25112511

25122512
void simplifyTypedef105() { // ticket #3616 (segmentation fault)
25132513
const char code[] = "( int typedef char x; ){}";
2514-
tok(code);
2515-
ASSERT_EQUALS("", errout_str());
2514+
ASSERT_THROW(tok(code), InternalError);
25162515
}
25172516

25182517
void simplifyTypedef106() { // ticket #3619 (segmentation fault)

test/testsymboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2530,7 +2530,7 @@ class TestSymbolDatabase : public TestFixture {
25302530
"static const std::string j;\n"
25312531
"const std::string* k;\n"
25322532
"const char m[];\n"
2533-
"void f(const char* const l;) {}");
2533+
"void f(const char* const l) {}");
25342534

25352535
ASSERT(db && db->variableList().size() == 6 && db->getVariableFromVarId(1) && db->getVariableFromVarId(2) && db->getVariableFromVarId(3) && db->getVariableFromVarId(4) && db->getVariableFromVarId(5));
25362536

test/testtokenize.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,10 +3692,10 @@ class TestTokenizer : public TestFixture {
36923692
void simplifyFunctionPointers3() {
36933693
// Related with ticket #2873
36943694
const char code[] = "void f() {\n"
3695-
"(void)(xy(*p)(0);)"
3695+
"(void)(xy(*p)(0));"
36963696
"\n}";
36973697
const char expected[] = "void f ( ) {\n"
3698-
"( void ) ( xy ( * p ) ( 0 ) ; )\n"
3698+
"( void ) ( xy ( * p ) ( 0 ) ) ;\n"
36993699
"}";
37003700
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
37013701
}
@@ -7589,8 +7589,9 @@ class TestTokenizer : public TestFixture {
75897589
}
75907590

75917591
void checkConfiguration() {
7592-
ASSERT_THROW(checkConfig("void f() { DEBUG(x();y()); }"), InternalError);
7593-
ASSERT_EQUALS("[test.cpp:1]: (information) Ensure that 'DEBUG' is defined either using -I, --include or -D.\n", errout_str());
7592+
ASSERT_THROW_EQUALS(checkConfig("void f() { DEBUG(x();y()); }"),
7593+
InternalError,
7594+
"There is an unknown macro here somewhere. Configuration is required. If DEBUG is a macro then please configure it.");
75947595
}
75957596

75967597
void unknownType() { // #8952

0 commit comments

Comments
 (0)