CheckBool::checkAssignBoolToFloat() - don't crash on unknown variables

This commit is contained in:
Alexander Mai 2014-05-24 19:15:43 +02:00
parent 38aaa46804
commit e6a394d514
2 changed files with 8 additions and 2 deletions

View File

@ -485,8 +485,8 @@ void CheckBool::checkAssignBoolToFloat()
const Scope * scope = symbolDatabase->functionScopes[i]; const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "!!* %var% = %bool% ;")) { if (Token::Match(tok, "!!* %var% = %bool% ;")) {
const Variable *var = symbolDatabase->getVariableFromVarId(tok->next()->varId()); const Variable * const var = symbolDatabase->getVariableFromVarId(tok->next()->varId());
if (var->isFloatingType()) if (var && var->isFloatingType())
assignBoolToFloatError(tok->next()); assignBoolToFloatError(tok->next());
} }
} }

View File

@ -155,6 +155,12 @@ private:
" long double d = (2>1);\n" " long double d = (2>1);\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Boolean value assigned to floating point variable.\n", "", errout.str()); TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Boolean value assigned to floating point variable.\n", "", errout.str());
// stability - don't crash:
check("void foo4() {\n"
" unknown = false;\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void comparisonOfBoolExpressionWithInt1() { void comparisonOfBoolExpressionWithInt1() {