diff --git a/lib/bughuntingchecks.cpp b/lib/bughuntingchecks.cpp index eb38858e1..afb55c5f7 100644 --- a/lib/bughuntingchecks.cpp +++ b/lib/bughuntingchecks.cpp @@ -269,6 +269,10 @@ static void uninit(const Token *tok, const ExprEngine::Value &value, ExprEngine: if (Token::Match(tok, "%var% .") && tok->next()->originalName() != "->") return; + // Assume that stream object is initialized + if (Token::Match(tok->previous(), "[;{}] %var% <<|>>") && !tok->next()->astParent()) + return; + // Containers are not uninitialized std::vector tokens{tok, tok->astOperand1(), tok->astOperand2()}; if (Token::Match(tok->previous(), ". %name%")) diff --git a/lib/exprengine.cpp b/lib/exprengine.cpp index f0ad91118..59fc71ffa 100644 --- a/lib/exprengine.cpp +++ b/lib/exprengine.cpp @@ -2241,6 +2241,10 @@ static std::string execute(const Token *start, const Token *end, Data &data) if (Token::Match(tok, "[;{}]")) data.trackProgramState(tok); + if (Token::simpleMatch(tok, "__CPPCHECK_BAILOUT__ ;")) + // This is intended for testing + throw ExprEngineException(tok, "__CPPCHECK_BAILOUT__"); + if (Token::simpleMatch(tok, "while (") && (tok->linkAt(1), ") ;") && tok->next()->astOperand1()->hasKnownIntValue() && tok->next()->astOperand1()->getKnownIntValue() == 0) { tok = tok->tokAt(4); continue; diff --git a/test/testbughuntingchecks.cpp b/test/testbughuntingchecks.cpp index 392991b31..0e2d7ccc1 100644 --- a/test/testbughuntingchecks.cpp +++ b/test/testbughuntingchecks.cpp @@ -40,6 +40,7 @@ private: TEST_CASE(uninit_function_par); TEST_CASE(uninit_malloc); TEST_CASE(uninit_struct); + TEST_CASE(uninit_bailout); TEST_CASE(ctu); #endif } @@ -110,6 +111,22 @@ private: ASSERT_EQUALS("", errout.str()); } + void uninit_bailout() { + check("void foo() {\n" + " __CPPCHECK_BAILOUT__;\n" + " int values[5];\n" + " values[i] = 123;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void foo() {\n" + " __CPPCHECK_BAILOUT__;\n" + " std::ostringstream comm;\n" + " comm << 123;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void ctu() { check("void init(int &x) {\n" " x = 1;\n"