Skip to content

Commit 4efb001

Browse files
Fix #12653, #12655 fuzzing crashes (#6339)
1 parent 3ca8997 commit 4efb001

9 files changed

Lines changed: 26 additions & 32 deletions

lib/tokenize.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5514,6 +5514,18 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
55145514
// @..
55155515
simplifyAt();
55165516

5517+
// Remove __declspec()
5518+
simplifyDeclspec();
5519+
5520+
// Remove "inline", "register", and "restrict"
5521+
simplifyKeyword();
5522+
5523+
// Remove [[attribute]]
5524+
simplifyCPPAttribute();
5525+
5526+
// remove __attribute__((?))
5527+
simplifyAttribute();
5528+
55175529
// Bail out if code is garbage
55185530
if (mTimerResults) {
55195531
Timer t("Tokenizer::simplifyTokens1::simplifyTokenList1::findGarbageCode", mSettings.showtime, mTimerResults);
@@ -5548,12 +5560,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
55485560
// simplify namespace aliases
55495561
simplifyNamespaceAliases();
55505562

5551-
// Remove [[attribute]]
5552-
simplifyCPPAttribute();
5553-
5554-
// remove __attribute__((?))
5555-
simplifyAttribute();
5556-
55575563
// simplify cppcheck attributes __cppcheck_?__(?)
55585564
simplifyCppcheckAttribute();
55595565

@@ -5595,13 +5601,8 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
55955601
if (Settings::terminated())
55965602
return false;
55975603

5598-
// Remove __declspec()
5599-
simplifyDeclspec();
56005604
validate();
56015605

5602-
// Remove "inline", "register", and "restrict"
5603-
simplifyKeyword();
5604-
56055606
// simplify simple calculations inside <..>
56065607
if (isCPP()) {
56075608
Token *lt = nullptr;
@@ -8656,7 +8657,7 @@ void Tokenizer::findGarbageCode() const
86568657
syntaxError(tok);
86578658
if (Token::Match(tok, "%cop%|= ]") && !(isCPP() && Token::Match(tok->previous(), "%type%|[|,|%num% &|=|> ]")))
86588659
syntaxError(tok);
8659-
if (Token::Match(tok, "[+-] [;,)]}]") && !(isCPP() && Token::Match(tok->previous(), "operator [+-] ;")))
8660+
if (Token::Match(tok, "[+-] [;,)]}]") && !(isCPP() && Token::simpleMatch(tok->previous(), "operator")))
86608661
syntaxError(tok);
86618662
if (Token::simpleMatch(tok, ",") &&
86628663
!Token::Match(tok->tokAt(-2), "[ = , &|%name%")) {
@@ -8696,6 +8697,8 @@ void Tokenizer::findGarbageCode() const
86968697
syntaxError(tok);
86978698
if (Token::Match(tok, "! %comp%"))
86988699
syntaxError(tok);
8700+
if (Token::Match(tok, "] %name%") && (!isCPP() || !(tok->tokAt(-1) && Token::simpleMatch(tok->tokAt(-2), "delete ["))))
8701+
syntaxError(tok);
86998702

87008703
if (tok->link() && Token::Match(tok, "[([]") && (!tok->tokAt(-1) || !tok->tokAt(-1)->isControlFlowKeyword())) {
87018704
const Token* const end = tok->link();
@@ -9226,7 +9229,7 @@ void Tokenizer::simplifyCppcheckAttribute()
92269229

92279230
void Tokenizer::simplifyCPPAttribute()
92289231
{
9229-
if (!isCPP() || mSettings.standards.cpp < Standards::CPP11)
9232+
if (!isCPP())
92309233
return;
92319234

92329235
for (Token *tok = list.front(); tok;) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__thread<>e
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
i n(){$ i;f a;for(i:++)a=[]i;}

test/testgarbage.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,8 +1609,7 @@ class TestGarbage : public TestFixture {
16091609

16101610
// #8752
16111611
void garbageCode199() {
1612-
checkCode("d f(){e n00e0[]n00e0&" "0+f=0}");
1613-
ignore_errout(); // we do not care about the output
1612+
ASSERT_THROW_INTERNAL(checkCode("d f(){e n00e0[]n00e0&" "0+f=0}"), SYNTAX);
16141613
}
16151614

16161615
// #8757
@@ -1715,8 +1714,7 @@ class TestGarbage : public TestFixture {
17151714
}
17161715

17171716
void garbageCode218() { // #8763
1718-
checkCode("d f(){t n0000 const[]n0000+0!=n0000,(0)}"); // don't crash
1719-
ignore_errout(); // we are not interested in the output
1717+
ASSERT_THROW_INTERNAL(checkCode("d f(){t n0000 const[]n0000+0!=n0000,(0)}"), SYNTAX);
17201718
}
17211719
void garbageCode219() { // #10101
17221720
checkCode("typedef void (*func) (addr) ;\n"

test/testleakautovar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ class TestLeakAutoVar : public TestFixture {
15401540
" delete[] p;\n"
15411541
" if (x && (p = new char[10]))\n"
15421542
" delete[] p;\n"
1543-
"}");
1543+
"}", /*cpp*/ true);
15441544
ASSERT_EQUALS("", errout_str());
15451545
}
15461546

test/testmemleak.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,8 +2105,6 @@ class TestMemleakStructMember : public TestFixture {
21052105

21062106
check(code2, true);
21072107
ASSERT_EQUALS("", errout_str());
2108-
check(code2, false);
2109-
ASSERT_EQUALS("", errout_str());
21102108

21112109
// Test unknown struct. In C++, it might have a destructor
21122110
const char code3[] = "void func() {\n"

test/testsimplifytemplate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5273,11 +5273,11 @@ class TestSimplifyTemplate : public TestFixture {
52735273

52745274
void template_array_type() {
52755275
ASSERT_EQUALS("void foo<int[]> ( int [ ] x ) ; "
5276-
"void bar ( ) { int [ 3 ] y ; foo<int[]> ( y ) ; } "
5276+
"void bar ( ) { int y [ 3 ] ; foo<int[]> ( y ) ; } "
52775277
"void foo<int[]> ( int [ ] x ) { }",
52785278
tok("template <class T> void foo(T x) {};\n"
52795279
"void bar() {\n"
5280-
" int[3] y;\n"
5280+
" int y[3];\n"
52815281
" foo<int[]>(y);\n"
52825282
"}"));
52835283
ASSERT_EQUALS("struct A<int[2]> ; "

test/testsymboldatabase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8603,7 +8603,7 @@ class TestSymbolDatabase : public TestFixture {
86038603
" auto lambda = [&]() -> bool\n"
86048604
" {\n"
86058605
" float x = 1.0f;\n"
8606-
" }\n"
8606+
" };\n"
86078607
" lambda();\n"
86088608
"}");
86098609

@@ -8618,7 +8618,7 @@ class TestSymbolDatabase : public TestFixture {
86188618

86198619
void lambda3() {
86208620
GET_SYMBOL_DB("void func() {\n"
8621-
" auto f = []() mutable {}\n"
8621+
" auto f = []() mutable {};\n"
86228622
"}");
86238623

86248624
ASSERT(db && db->scopeList.size() == 3);

test/testtokenize.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5818,21 +5818,14 @@ class TestTokenizer : public TestFixture {
58185818
ASSERT_EQUALS("int f ( ) ;",
58195819
tokenizeAndStringify("[[deprecated]] int f();"));
58205820

5821-
ASSERT_EQUALS("[ [ deprecated ] ] int f ( ) ;",
5822-
tokenizeAndStringify("[[deprecated]] int f();", true, Platform::Type::Native, true, Standards::CPP03));
5823-
5824-
ASSERT_EQUALS("[ [ deprecated ] ] int f ( ) ;",
5825-
tokenizeAndStringify("[[deprecated]] int f();", true, Platform::Type::Native, false));
5821+
ASSERT_THROW_INTERNAL(tokenizeAndStringify("[[deprecated]] int f();", true, Platform::Type::Native, false), SYNTAX);
58265822

58275823
ASSERT_EQUALS("template < class T > int f ( ) { }",
58285824
tokenizeAndStringify("template <class T> [[noreturn]] int f(){}"));
58295825

58305826
ASSERT_EQUALS("int f ( int i ) ;",
58315827
tokenizeAndStringify("[[maybe_unused]] int f([[maybe_unused]] int i);"));
58325828

5833-
ASSERT_EQUALS("[ [ maybe_unused ] ] int f ( [ [ maybe_unused ] ] int i ) ;",
5834-
tokenizeAndStringify("[[maybe_unused]] int f([[maybe_unused]] int i);", true, Platform::Type::Native, true, Standards::CPP03));
5835-
58365829
ASSERT_EQUALS("struct a ;",
58375830
tokenizeAndStringify("struct [[]] a;"));
58385831

0 commit comments

Comments
 (0)