CheckUninitVar: Fix crash when AST is not created properly

This commit is contained in:
Daniel Marjamäki 2015-07-22 22:17:12 +02:00
parent 1f5eea98b5
commit 1de1aaa2f4
2 changed files with 10 additions and 1 deletions

View File

@ -1206,7 +1206,7 @@ static bool isVariableUsed(const Token *tok, const Variable& var)
return false; return false;
if (tok->str() == "&" && !tok->astOperand2()) if (tok->str() == "&" && !tok->astOperand2())
return false; return false;
if (Token::Match(tok, "%cop%|:")) if (tok->isConstOp())
return isVariableUsed(tok->astOperand1(),var) || isVariableUsed(tok->astOperand2(),var); return isVariableUsed(tok->astOperand1(),var) || isVariableUsed(tok->astOperand2(),var);
return (tok->varId() == var.declarationId()); return (tok->varId() == var.declarationId());
} }
@ -1458,6 +1458,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
} }
if (tok->str() == "?") { if (tok->str() == "?") {
if (!tok->astOperand2())
return true;
const bool used1 = isVariableUsed(tok->astOperand2()->astOperand1(), var); const bool used1 = isVariableUsed(tok->astOperand2()->astOperand1(), var);
const bool used0 = isVariableUsed(tok->astOperand2()->astOperand2(), var); const bool used0 = isVariableUsed(tok->astOperand2()->astOperand2(), var);
const bool err = (number_of_if == 0) ? (used1 || used0) : (used1 && used0); const bool err = (number_of_if == 0) ? (used1 || used0) : (used1 && used0);
@ -1503,6 +1505,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
tok = tok->linkAt(1); tok = tok->linkAt(1);
else if (tok->str() == "?") { else if (tok->str() == "?") {
if (!tok->astOperand2())
return true;
const bool used1 = isVariableUsed(tok->astOperand2()->astOperand1(), var); const bool used1 = isVariableUsed(tok->astOperand2()->astOperand1(), var);
const bool used0 = isVariableUsed(tok->astOperand2()->astOperand2(), var); const bool used0 = isVariableUsed(tok->astOperand2()->astOperand2(), var);
const bool err = (number_of_if == 0) ? (used1 || used0) : (used1 && used0); const bool err = (number_of_if == 0) ? (used1 || used0) : (used1 && used0);

View File

@ -3129,6 +3129,11 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar2("void f() {\n" // Don't crash
" int a;\n"
" dostuff(\"ab\" cd \"ef\", x?a:z);\n" // <- No AST is created for ?:
"}\n");
// Unknown => bail out.. // Unknown => bail out..
checkUninitVarB("void f(int x) {\n" checkUninitVarB("void f(int x) {\n"
" int i;\n" " int i;\n"