From 305609f4fb5b473df978ba83dc88fc13849844ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 17 Aug 2016 18:44:41 +0200 Subject: [PATCH] Fixed #7688 (setTokenValue: crash when there is no 2nd operand for ternary operator) --- lib/valueflow.cpp | 11 ++++++++--- test/testvalueflow.cpp | 5 +++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index fed2be696..4f882eb5d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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 &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 &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 tokens; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 6626573bc..7d5b5d3d2 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -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"