Skip to content

Commit 1dac166

Browse files
committed
7357 segmentation fault in TokenList::insertTokens
1 parent 462b98a commit 1dac166

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

lib/tokenize.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5597,6 +5597,8 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_
55975597
const Token *varTok = type0->tokAt((int)typelen);
55985598
while (Token::Match(varTok, "*|&|const"))
55995599
varTok = varTok->next();
5600+
if (!varTok)
5601+
syntaxError(tok2); // invalid code
56005602
list.insertTokens(eq, varTok, 2);
56015603
eq->str(";");
56025604

test/testgarbage.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class TestGarbage : public TestFixture {
220220
TEST_CASE(garbageCode169); // #6731
221221
TEST_CASE(garbageCode170);
222222
TEST_CASE(garbageCode171);
223+
TEST_CASE(garbageCode172);
223224
TEST_CASE(garbageValueFlow);
224225
TEST_CASE(garbageSymbolDatabase);
225226
TEST_CASE(garbageAST);
@@ -228,10 +229,10 @@ class TestGarbage : public TestFixture {
228229

229230
std::string checkCode(const char code[], bool cpp = true) {
230231
// double the tests - run each example as C as well as C++
231-
const char* filename = cpp ? "test.cpp" : "test.c";
232-
const char* alternatefilename = cpp ? "test.c" : "test.cpp";
232+
const char* const filename = cpp ? "test.cpp" : "test.c";
233+
const char* const alternatefilename = cpp ? "test.c" : "test.cpp";
233234

234-
// run alternate check first. It should only ensure stability
235+
// run alternate check first. It should only ensure stability - so we catch exceptions here.
235236
try {
236237
checkCodeInternal(code, alternatefilename);
237238
} catch (InternalError&) {
@@ -1448,6 +1449,11 @@ class TestGarbage : public TestFixture {
14481449
ASSERT_THROW(checkCode("(){case()?():}:", false), InternalError);
14491450
}
14501451

1452+
void garbageCode172() {
1453+
// #7357
1454+
ASSERT_THROW(checkCode("p<e T=l[<]<>>,"), InternalError);
1455+
}
1456+
14511457
};
14521458

14531459
REGISTER_TEST(TestGarbage)

0 commit comments

Comments
 (0)