Skip to content

Commit cbf0b13

Browse files
committed
Ticket #8255: Don't crash upon invalid code while checking conditions.
1 parent 883da93 commit cbf0b13

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

lib/tokenize.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8310,6 +8310,8 @@ const Token * Tokenizer::findGarbageCode() const
83108310
for (const Token *tok = tokens(); tok; tok = tok->next()) {
83118311
if (Token::Match(tok, "%cop% %or%|%oror%|/|%"))
83128312
return tok;
8313+
if (Token::Match(tok, ";|(|[ %comp%"))
8314+
return tok;
83138315
}
83148316

83158317
// Code must not start with an arithmetical operand

test/testgarbage.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class TestGarbage : public TestFixture {
218218
TEST_CASE(garbageCode185); // #6011
219219
TEST_CASE(garbageCode186); // #8151
220220
TEST_CASE(garbageCode187);
221+
TEST_CASE(garbageCode188);
221222
TEST_CASE(garbageValueFlow);
222223
TEST_CASE(garbageSymbolDatabase);
223224
TEST_CASE(garbageAST);
@@ -562,12 +563,12 @@ class TestGarbage : public TestFixture {
562563
}
563564

564565
void garbageCode36() { // #6334
565-
checkCode("{ } < class template < > , { = } ; class... >\n"
566-
"struct Y { }\n"
567-
"class Types { }\n"
568-
"( X < int > \"uses template\" ) ( < ( ) \"uses ;"
569-
"( int int ::primary \"uses template\" ) int double \"uses )"
570-
"::primary , \"uses template\" ;\n");
566+
ASSERT_THROW(checkCode("{ } < class template < > , { = } ; class... >\n"
567+
"struct Y { }\n"
568+
"class Types { }\n"
569+
"( X < int > \"uses template\" ) ( < ( ) \"uses ;"
570+
"( int int ::primary \"uses template\" ) int double \"uses )"
571+
"::primary , \"uses template\" ;\n"), InternalError);
571572
}
572573

573574
void garbageCode37() {
@@ -710,7 +711,7 @@ class TestGarbage : public TestFixture {
710711
}
711712

712713
void garbageCode77() { // #6755
713-
checkCode("void foo (int **p) { { { };>= } } unsigned *d = (b b--) --*d");
714+
ASSERT_THROW(checkCode("void foo (int **p) { { { };>= } } unsigned *d = (b b--) --*d"), InternalError);
714715
}
715716

716717
void garbageCode78() { // #6756
@@ -823,7 +824,7 @@ class TestGarbage : public TestFixture {
823824
}
824825

825826
void garbageCode105() { // #6859
826-
checkCode("void foo (int i) { int a , for (a 1; a( < 4; a++) if (a) (b b++) (b);) n++; }");
827+
ASSERT_THROW(checkCode("void foo (int i) { int a , for (a 1; a( < 4; a++) if (a) (b b++) (b);) n++; }"), InternalError);
827828
}
828829

829830
void garbageCode106() { // #6880
@@ -843,7 +844,7 @@ class TestGarbage : public TestFixture {
843844
}
844845

845846
void garbageCode110() { // #6902 "segmentation fault (invalid code) in CheckStl::string_c_str"
846-
checkCode("( *const<> ( size_t ) ; foo ) { } * ( *const ( size_t ) ( ) ;> foo )< { }");
847+
ASSERT_THROW(checkCode("( *const<> ( size_t ) ; foo ) { } * ( *const ( size_t ) ( ) ;> foo )< { }"), InternalError);
847848
}
848849

849850
void garbageCode111() { // #6907
@@ -866,7 +867,7 @@ class TestGarbage : public TestFixture {
866867
}
867868

868869
void garbageCode116() { // #5356
869-
checkCode("struct template<int { = }; > struct B { }; B < 0 > b;");
870+
ASSERT_THROW(checkCode("struct template<int { = }; > struct B { }; B < 0 > b;"), InternalError);
870871
}
871872

872873
void garbageCode117() { // #6121
@@ -1097,7 +1098,7 @@ class TestGarbage : public TestFixture {
10971098
}
10981099

10991100
void garbageCode144() { // #6865
1100-
checkCode("template < typename > struct A { } ; template < typename > struct A < INVALID > : A < int[ > { }] ;");
1101+
ASSERT_THROW(checkCode("template < typename > struct A { } ; template < typename > struct A < INVALID > : A < int[ > { }] ;"), InternalError);
11011102
}
11021103

11031104
void garbageCode146() { // #7081
@@ -1432,6 +1433,10 @@ class TestGarbage : public TestFixture {
14321433
ASSERT_THROW(checkCode(inp), InternalError);
14331434
}
14341435

1436+
void garbageCode188() { // #8255
1437+
ASSERT_THROW(checkCode("{z r(){(){for(;<(x);){if(0==0)}}}}"), InternalError);
1438+
}
1439+
14351440
void syntaxErrorFirstToken() {
14361441
ASSERT_THROW(checkCode("&operator(){[]};"), InternalError); // #7818
14371442
ASSERT_THROW(checkCode("*(*const<> (size_t); foo) { } *(*const (size_t)() ; foo) { }"), InternalError); // #6858

0 commit comments

Comments
 (0)