diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index df2e12fbf..925af5c84 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9523,8 +9523,9 @@ void Tokenizer::simplifyBitfields() offset = 1; Token *tok1 = tok->tokAt(2 + offset); - if (tok1->tokAt(2)->isBoolean() || Token::Match(tok1->tokAt(2), "%num%") || - !Token::Match(tok1->tokAt(2), "public|protected|private| %type% ::|<|,|{|;")) + if (tok1 && tok1->tokAt(2) && + (tok1->tokAt(2)->isBoolean() || Token::Match(tok1->tokAt(2), "%num%") || + !Token::Match(tok1->tokAt(2), "public|protected|private| %type% ::|<|,|{|;"))) { while (tok1->next() && !Token::Match(tok1->next(), ";|,")) tok1->deleteNext(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d0b625c86..ae977db88 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -305,6 +305,7 @@ private: TEST_CASE(bitfields8); TEST_CASE(bitfields9); // ticket #2706 TEST_CASE(bitfields10); + TEST_CASE(bitfields11); // ticket #2845 (segmentation fault) TEST_CASE(microsoftMFC); @@ -5356,6 +5357,14 @@ private: ASSERT_EQUALS("{ } MACRO default : { } ;", tokenizeAndStringify(code,false)); } + void bitfields11() // ticket #2845 (segmentation fault) + { + const char code[] = "#if b&&a\n" + "#ifdef y z:\n"; + tokenizeAndStringify(code,false); + ASSERT_EQUALS("", errout.str()); + } + void microsoftMFC() { const char code1[] = "class MyDialog : public CDialog { DECLARE_MESSAGE_MAP() private: CString text; };";