diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 2d9e5569e..7036417ae 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8215,6 +8215,12 @@ const Token * Tokenizer::findGarbageCode() const } } + // Operators without operands.. + for (const Token *tok = tokens(); tok; tok = tok->next()) { + if (Token::Match(tok, "%cop% %or%|%oror%|/|%")) + return tok; + } + // Code must not start with an arithmetical operand if (Token::Match(list.front(), "%cop%")) return list.front(); diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index a04b5333a..71f0f9eb6 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -217,6 +217,7 @@ private: TEST_CASE(garbageCode184); // #7699 TEST_CASE(garbageCode185); // #6011 TEST_CASE(garbageCode186); // #8151 + TEST_CASE(garbageCode187); TEST_CASE(garbageValueFlow); TEST_CASE(garbageSymbolDatabase); TEST_CASE(garbageAST); @@ -226,7 +227,7 @@ private: TEST_CASE(enumTrailingComma); } - std::string checkCode(const char code[], bool cpp = true) { + std::string checkCode(const std::string &code, bool cpp = true) { // double the tests - run each example as C as well as C++ const char* const filename = cpp ? "test.cpp" : "test.c"; const char* const alternatefilename = cpp ? "test.c" : "test.cpp"; @@ -240,7 +241,7 @@ private: return checkCodeInternal(code, filename); } - std::string checkCodeInternal(const char code[], const char* filename) { + std::string checkCodeInternal(const std::string &code, const char* filename) { errout.str(""); // tokenize.. @@ -833,7 +834,7 @@ private: } void garbageCode108() { // #6895 "segmentation fault (invalid code) in CheckCondition::isOppositeCond" - checkCode("A( ) { } bool f( ) { ( ) F; ( ) { ( == ) if ( !=< || ( !A( ) && r[2] ) ) ( !A( ) ) ( ) } }"); + ASSERT_THROW(checkCode("A( ) { } bool f( ) { ( ) F; ( ) { ( == ) if ( !=< || ( !A( ) && r[2] ) ) ( !A( ) ) ( ) } }"), InternalError); } void garbageCode109() { // #6900 "segmentation fault (invalid code) in CheckStl::runSimplifiedChecks" @@ -1425,6 +1426,10 @@ private: ASSERT_THROW(checkCode("A<>C"), InternalError); } + void garbageCode187() { // # 8152 - segfault in handling + const std::string inp("0|\0|0>;\n", 8); + ASSERT_THROW(checkCode(inp), InternalError); + } void syntaxErrorFirstToken() { ASSERT_THROW(checkCode("&operator(){[]};"), InternalError); // #7818