Skip to content

Commit 932ba10

Browse files
committed
Reject invalid code after if/switch/loop (also fixes #14326)
1 parent 602da94 commit 932ba10

5 files changed

Lines changed: 14 additions & 9 deletions

File tree

lib/tokenize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8664,6 +8664,9 @@ void Tokenizer::findGarbageCode() const
86648664
}
86658665
if (!Token::Match(tok->next(), "( !!)"))
86668666
syntaxError(tok);
8667+
if (Token::simpleMatch(tok->linkAt(1), ") }")) {
8668+
syntaxError(tok->linkAt(1)->next());
8669+
}
86678670
if (tok->str() != "for") {
86688671
if (isGarbageExpr(tok->next(), tok->linkAt(1), cpp && (mSettings.standards.cpp>=Standards::cppstd_t::CPP17)))
86698672
syntaxError(tok);

test/testcondition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ 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

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
@@ -3672,8 +3672,7 @@ class TestSymbolDatabase : public TestFixture {
36723672
}
36733673

36743674
void symboldatabase36() { // ticket #4892
3675-
check("void struct ( ) { if ( 1 ) } int main ( ) { }");
3676-
ASSERT_EQUALS("", errout_str());
3675+
ASSERT_THROW_INTERNAL(check("void struct ( ) { if ( 1 ) } int main ( ) { }"), SYNTAX);
36773676
}
36783677

36793678
void symboldatabase37() {

test/testtokenize.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5345,8 +5345,8 @@ class TestTokenizer : public TestFixture {
53455345
ASSERT_EQUALS("; x = 123 ;", tokenizeAndStringify(";x=({123;});"));
53465346
ASSERT_EQUALS("; x = y ;", tokenizeAndStringify(";x=({y;});"));
53475347
// #13419: Do not simplify compound statements in for loop
5348-
ASSERT_EQUALS("void foo ( int x ) { for ( ; ( { { } ; x < 1 ; } ) ; ) }",
5349-
tokenizeAndStringify("void foo(int x) { for (;({ {}; x<1; });) }"));
5348+
ASSERT_EQUALS("void foo ( int x ) { for ( ; ( { { } ; x < 1 ; } ) { ; } ) }",
5349+
tokenizeAndStringify("void foo(int x) { for (;({ {}; x<1; });); }"));
53505350
}
53515351

53525352
void simplifyOperatorName1() {
@@ -7627,6 +7627,11 @@ class TestTokenizer : public TestFixture {
76277627

76287628
ASSERT_THROW_INTERNAL(tokenizeAndStringify("{ for (()()) }"), SYNTAX); // #11643
76297629

7630+
ASSERT_THROW_INTERNAL(tokenizeAndStringify("void f(const std::vector<std::string>& v) {\n" // #14326
7631+
" for (const std::string&s : v)\n"
7632+
"}"),
7633+
SYNTAX);
7634+
76307635
ASSERT_NO_THROW(tokenizeAndStringify("S* g = ::new(ptr) S();")); // #12552
76317636
ASSERT_NO_THROW(tokenizeAndStringify("void f(int* p) { return ::delete p; }"));
76327637

0 commit comments

Comments
 (0)