From 1de1aaa2f43b6b038ec25ca6c000b80792c1806c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 22 Jul 2015 22:17:12 +0200 Subject: [PATCH] CheckUninitVar: Fix crash when AST is not created properly --- lib/checkuninitvar.cpp | 6 +++++- test/testuninitvar.cpp | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 66c6fda58..d6e88835a 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1206,7 +1206,7 @@ static bool isVariableUsed(const Token *tok, const Variable& var) return false; if (tok->str() == "&" && !tok->astOperand2()) return false; - if (Token::Match(tok, "%cop%|:")) + if (tok->isConstOp()) return isVariableUsed(tok->astOperand1(),var) || isVariableUsed(tok->astOperand2(),var); return (tok->varId() == var.declarationId()); } @@ -1458,6 +1458,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var } if (tok->str() == "?") { + if (!tok->astOperand2()) + return true; const bool used1 = isVariableUsed(tok->astOperand2()->astOperand1(), var); const bool used0 = isVariableUsed(tok->astOperand2()->astOperand2(), var); 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); else if (tok->str() == "?") { + if (!tok->astOperand2()) + return true; const bool used1 = isVariableUsed(tok->astOperand2()->astOperand1(), var); const bool used0 = isVariableUsed(tok->astOperand2()->astOperand2(), var); const bool err = (number_of_if == 0) ? (used1 || used0) : (used1 && used0); diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index f2a3dff4a..b18b5ec28 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -3129,6 +3129,11 @@ private: "}\n"); 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.. checkUninitVarB("void f(int x) {\n" " int i;\n"