From 7a7257f200c8a8d5e40fafe96c331356f64acc9c Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Tue, 8 Mar 2011 20:24:57 -0500 Subject: [PATCH] fix #2630 (segmentation fault of cppcheck ( typedef ... ) --- lib/tokenize.cpp | 7 +++++++ test/testsimplifytokens.cpp | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e23d9a605..9514dcd12 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1151,6 +1151,13 @@ void Tokenizer::simplifyTypedef() while (Token::Match(tok->tokAt(offset), "*|&|const")) pointers.push_back(tok->tokAt(offset++)->str()); + // check for invalid input + if (!tok->tokAt(offset)) + { + syntaxError(tok); + return; + } + if (Token::Match(tok->tokAt(offset), "%type%")) { // found the type name diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 2a3a17b2e..74adc52af 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -4973,8 +4973,16 @@ private: void simplifyTypedef84() // ticket #2630 (segmentation fault) { - const char code[] = "typedef y x () x\n"; - checkSimplifyTypedef(code); + const char code1[] = "typedef y x () x\n"; + checkSimplifyTypedef(code1); + ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); + + const char code2[] = "typedef struct template <>\n"; + checkSimplifyTypedef(code2); + ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); + + const char code3[] = "typedef ::<>\n"; + checkSimplifyTypedef(code3); ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); }