From 9bf6e6789892d62d0bf9e65df04b763e9a5e0298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 24 Feb 2011 18:49:16 +0100 Subject: [PATCH] Fixed #2610 (segmentation fault of cppcheck ( if () < {} )) --- lib/tokenize.cpp | 2 ++ test/testtokenize.cpp | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e44fe681a..8a3dbdb9f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4663,6 +4663,8 @@ void Tokenizer::simplifyIfAddBraces() break; } tempToken = tempToken->link(); + if (!tempToken || !tempToken->next()) + break; if (tempToken->next()->isName() && tempToken->next()->str() != "else") break; continue; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7b35ea570..02cf0cc71 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -79,6 +79,7 @@ private: TEST_CASE(ifAddBraces11); TEST_CASE(ifAddBraces12); TEST_CASE(ifAddBraces13); + TEST_CASE(ifAddBraces14); // #2610 - segfault: if()<{} TEST_CASE(whileAddBraces); TEST_CASE(doWhileAddBraces); @@ -850,6 +851,13 @@ private: ASSERT_EQUALS(expected2, tokenizeAndStringify(code2, true)); } + void ifAddBraces14() + { + // ticket #2610 (segfault) + tokenizeAndStringify("if()<{}", false); + } + + void whileAddBraces() { const char code[] = ";while(a);";