Fixed #7688 (setTokenValue: crash when there is no 2nd operand for ternary operator)

This commit is contained in:
Daniel Marjamäki 2016-08-17 18:44:41 +02:00
parent 499e52c976
commit 305609f4fb
2 changed files with 13 additions and 3 deletions

View File

@ -367,9 +367,14 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
if (parent->astOperand1()->values.size() == 1U && parent->astOperand1()->values.front().isKnown()) {
const ValueFlow::Value &condvalue = parent->astOperand1()->values.front();
const bool cond(condvalue.tokvalue || condvalue.intvalue != 0);
const std::list<ValueFlow::Value> &values = cond ? tok->astOperand1()->values : tok->astOperand2()->values;
if (std::find(values.begin(), values.end(), value) != values.end())
setTokenValue(parent, value);
if (cond && !tok->astOperand1()) { // true condition, no second operator
setTokenValue(parent, condvalue);
} else {
const Token *op = cond ? tok->astOperand1() : tok->astOperand2();
const std::list<ValueFlow::Value> &values = op->values;
if (std::find(values.begin(), values.end(), value) != values.end())
setTokenValue(parent, value);
}
} else {
// is condition only depending on 1 variable?
std::stack<const Token*> tokens;

View File

@ -332,6 +332,11 @@ private:
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(2, values.front().intvalue);
code = "x = 123 ? : 456;\n";
values = tokenValues(code, "?");
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(123, values.empty() ? 0 : values.front().intvalue);
// !
code = "void f(int x) {\n"
" a = !x;\n"