Skip to content

Commit 9dfee85

Browse files
Reject invalid code after if/switch/loop (also fixes #14326) (#8033)
Co-authored-by: chrchr-github <[email protected]>
1 parent 9589549 commit 9dfee85

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

lib/tokenize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8672,6 +8672,9 @@ void Tokenizer::findGarbageCode() const
86728672
}
86738673
if (!Token::Match(tok->next(), "( !!)"))
86748674
syntaxError(tok);
8675+
if (Token::simpleMatch(tok->linkAt(1), ") }")) {
8676+
syntaxError(tok->linkAt(1)->next());
8677+
}
86758678
if (tok->str() != "for") {
86768679
if (isGarbageExpr(tok->next(), tok->linkAt(1), cpp && (mSettings.standards.cpp>=Standards::cppstd_t::CPP17)))
86778680
syntaxError(tok);

test/testcondition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,13 @@ class TestCondition : public TestFixture {
771771

772772
check("void f(size_t x) {\n"
773773
" if (x == sizeof(int)) {}\n"
774-
" else { if (x == sizeof(long))} {}\n"
774+
" else { if (x == sizeof(long)) {} }\n"
775775
"}\n");
776776
ASSERT_EQUALS("", errout_str());
777777

778778
check("void f(size_t x) {\n"
779779
" if (x == sizeof(long)) {}\n"
780-
" else { if (x == sizeof(long long))} {}\n"
780+
" else { if (x == sizeof(long long)) {} }\n"
781781
"}\n");
782782
ASSERT_EQUALS("", errout_str());
783783
}

test/testgarbage.cpp

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

885885
void garbageCode102() { // #6846 (segmentation fault)
886-
(void)checkCode("struct Object { ( ) ; Object & operator= ( Object ) { ( ) { } if ( this != & b ) } }");
887-
ignore_errout(); // we do not care about the output
886+
ASSERT_THROW_INTERNAL(checkCode("struct Object { ( ) ; Object & operator= ( Object ) { ( ) { } if ( this != & b ) } }"), SYNTAX);
888887
}
889888

890889
void garbageCode103() { // #6824
@@ -1251,8 +1250,7 @@ class TestGarbage : public TestFixture {
12511250
const char code[] = "template <bool foo = std::value &&>\n"
12521251
"static std::string foo(char *Bla) {\n"
12531252
" while (Bla[1] && Bla[1] != ',') }\n";
1254-
(void)checkCode(code);
1255-
ignore_errout(); // we are not interested in the output
1253+
ASSERT_THROW_INTERNAL(checkCode(code), SYNTAX);
12561254
}
12571255

12581256
void garbageCode153() {

test/testsymboldatabase.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,8 +3674,7 @@ class TestSymbolDatabase : public TestFixture {
36743674
}
36753675

36763676
void symboldatabase36() { // ticket #4892
3677-
check("void struct ( ) { if ( 1 ) } int main ( ) { }");
3678-
ASSERT_EQUALS("", errout_str());
3677+
ASSERT_THROW_INTERNAL(check("void struct ( ) { if ( 1 ) } int main ( ) { }"), SYNTAX);
36793678
}
36803679

36813680
void symboldatabase37() {

test/testtokenize.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5365,8 +5365,8 @@ class TestTokenizer : public TestFixture {
53655365
ASSERT_EQUALS("; x = 123 ;", tokenizeAndStringify(";x=({123;});"));
53665366
ASSERT_EQUALS("; x = y ;", tokenizeAndStringify(";x=({y;});"));
53675367
// #13419: Do not simplify compound statements in for loop
5368-
ASSERT_EQUALS("void foo ( int x ) { for ( ; ( { { } ; x < 1 ; } ) ; ) }",
5369-
tokenizeAndStringify("void foo(int x) { for (;({ {}; x<1; });) }"));
5368+
ASSERT_EQUALS("void foo ( int x ) { for ( ; ( { { } ; x < 1 ; } ) ; ) { ; } }",
5369+
tokenizeAndStringify("void foo(int x) { for (;({ {}; x<1; });); }"));
53705370
}
53715371

53725372
void simplifyOperatorName1() {
@@ -7647,6 +7647,11 @@ class TestTokenizer : public TestFixture {
76477647

76487648
ASSERT_THROW_INTERNAL(tokenizeAndStringify("{ for (()()) }"), SYNTAX); // #11643
76497649

7650+
ASSERT_THROW_INTERNAL(tokenizeAndStringify("void f(const std::vector<std::string>& v) {\n" // #14326
7651+
" for (const std::string&s : v)\n"
7652+
"}"),
7653+
SYNTAX);
7654+
76507655
ASSERT_NO_THROW(tokenizeAndStringify("S* g = ::new(ptr) S();")); // #12552
76517656
ASSERT_NO_THROW(tokenizeAndStringify("void f(int* p) { return ::delete p; }"));
76527657

0 commit comments

Comments
 (0)