diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index bf7d13f8e..9fae3503c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3918,10 +3918,14 @@ void Tokenizer::simplifySizeof() --parlevel; if (Token::Match(tempToken, "%var%")) { - while (tempToken->next()->str() == "[") + while (tempToken && tempToken->next() && tempToken->next()->str() == "[") { tempToken = tempToken->next()->link(); } + if (!tempToken || !tempToken->next()) + { + break; + } if (tempToken->next()->str() == ".") { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index da1994a81..35f0d4e34 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -85,6 +85,7 @@ private: TEST_CASE(sizeof20); // #2024 - sizeof a) TEST_CASE(sizeof21); // #2232 - sizeof...(Args) TEST_CASE(sizeof22); // #2599 + TEST_CASE(sizeof23); // #2604 TEST_CASE(sizeofsizeof); TEST_CASE(casting); @@ -1433,6 +1434,15 @@ private: tok(code); } + void sizeof23() + { + // ticket #2604 segmentation fault + const char code[] = "sizeof <= A\n"; + + // don't segfault + tok(code); + } + void sizeofsizeof() {