Skip to content

Commit 38b2f6b

Browse files
amai2012danmar
authored andcommitted
Fixed cppcheck-opensource#5168 (segmentation fault (invalid code) in Tokenizer::simplifyVarDecl)
1 parent 8b77b77 commit 38b2f6b

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3175,7 +3175,7 @@ void Tokenizer::createLinks2()
31753175
if (token->link()) {
31763176
if (Token::Match(token, "{|[|("))
31773177
type.push(token);
3178-
else if (Token::Match(token, "}|]|)")) {
3178+
else if (!type.empty() && Token::Match(token, "}|]|)")) {
31793179
while (type.top()->str() == "<")
31803180
type.pop();
31813181
type.pop();

test/testsimplifytokens.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class TestSimplifyTokens : public TestFixture {
134134
TEST_CASE(template39); // #4742 - freeze
135135
TEST_CASE(template40); // #5055 - template specialization outside struct
136136
TEST_CASE(template41); // #4710 - const in instantiation not handled perfectly
137+
TEST_CASE(template42); // #4878 - variadic templates
137138
TEST_CASE(template_unhandled);
138139
TEST_CASE(template_default_parameter);
139140
TEST_CASE(template_default_type);
@@ -2350,6 +2351,19 @@ class TestSimplifyTokens : public TestFixture {
23502351
ASSERT_EQUALS("int x ( ) { return f<int> ( 123 ) ; } int f<int> ( int t ) { return t ; }", tok(code2));
23512352
}
23522353

2354+
void template42() { // #4878 cpcheck aborts in ext-blocks.cpp (clang testcode)
2355+
const char code[] = "template<typename ...Args>\n"
2356+
"int f0(Args ...args) {\n"
2357+
" return ^ {\n"
2358+
" return sizeof...(Args);\n"
2359+
" }() + ^ {\n"
2360+
" return sizeof...(args);\n"
2361+
" }();\n"
2362+
"}";
2363+
tok(code);
2364+
}
2365+
2366+
23532367
void template_default_parameter() {
23542368
{
23552369
const char code[] = "template <class T, int n=3>\n"

0 commit comments

Comments
 (0)