From bc8ecf2aa7ce4eec533b22c88aaca9a1ac6718c7 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Thu, 4 Nov 2010 17:54:04 +0100 Subject: [PATCH] Fixed #2168 (Segmentation fault of cppcheck) --- lib/checkpostfixoperator.cpp | 8 +++++++- test/testpostfixoperator.cpp | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/checkpostfixoperator.cpp b/lib/checkpostfixoperator.cpp index a835d7f99..9e354cc9f 100644 --- a/lib/checkpostfixoperator.cpp +++ b/lib/checkpostfixoperator.cpp @@ -37,7 +37,13 @@ void CheckPostfixOperator::postfixOperator() if (!_settings->_checkCodingStyle) return; - for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) + const Token *tok = _tokenizer->tokens(); + + // prevent crash if first token is ++ or -- + if (Token::Match(tok, "++|--")) + tok = tok->next(); + + for (; tok; tok = tok->next()) { bool result = false; if (Token::Match(tok, "++|--")) diff --git a/test/testpostfixoperator.cpp b/test/testpostfixoperator.cpp index a35e88300..d6a30ec65 100644 --- a/test/testpostfixoperator.cpp +++ b/test/testpostfixoperator.cpp @@ -67,6 +67,7 @@ private: TEST_CASE(teststream); TEST_CASE(testvolatile); TEST_CASE(testiterator); + TEST_CASE(test2168); } void testsimple() @@ -364,7 +365,12 @@ private: } - + void test2168() + { + check("--> declare allocator lock here\n" + "int main(){}\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestPostfixOperator)