diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index dca121f59..b0d8aebb6 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -50,7 +50,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog unsigned int varid; MathLib::bigint num; const Variable *var; - if (tok->isComparisonOp() && tok->astOperand1() && tok->astOperand2()) { + if (tok->isComparisonOp() && tok->astOperand2()) { if (tok->astOperand1()->isName() && tok->astOperand2()->isNumber()) { varid = tok->astOperand1()->varId(); var = tok->astOperand1()->variable(); @@ -78,8 +78,8 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog if (varid == 0U) continue; - // bailout: global variables - if (var && var->isGlobal()) { + // bailout: global non-const variables + if (var && var->isGlobal() && !var->isConst()) { if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok, "global variable " + var->nameToken()->str()); continue; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 4ab1bee8e..e71842c7c 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -97,6 +97,13 @@ private: "}"; ASSERT_EQUALS(true, testValueOfX(code, 2U, 0)); + code = "extern const int x;\n" + "void f() {\n" + " int a = x;\n" + " if (x == 123) {}\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code, 3U, 123)); + // bailout: ?: bailout("void f(int x) {\n" " y = ((x<0) ? x : ((x==2)?3:4));\n"