From 962bddf755b0685a0a0f6872287c64c3475f2c48 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 5 Oct 2023 17:50:32 +0200 Subject: [PATCH 1/6] Fix #12039 FP knownConditionTrueFalse --- lib/valueflow.cpp | 1 + test/testnullpointer.cpp | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6ac247684b9..13a8f0f324f 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1858,6 +1858,7 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings* sett for (const Token* tok2 : tokens) { if (tok2->hasKnownIntValue()) { values.emplace_back(); + values.back().intvalue = tok2->getKnownIntValue(); } else { ValueFlow::Value symValue{}; symValue.valueType = ValueFlow::Value::ValueType::SYMBOLIC; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 2c116825e21..758d02c7b23 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -2365,7 +2365,9 @@ class TestNullPointer : public TestFixture { " if (!flag1 && flag2)\n" " (*ptr)++;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:10]: (warning) Either the condition 'ptr!=nullptr' is redundant or there is possible null pointer dereference: ptr.\n", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:10]: (warning) Either the condition 'ptr!=nullptr' is redundant or there is possible null pointer dereference: ptr.\n", + "", + errout.str()); } void nullpointer74() { From 2ad9dcb1c5541e944ffddd532db7309822fd2760 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 5 Oct 2023 17:55:10 +0200 Subject: [PATCH 2/6] Add test --- test/testcondition.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 4cb72fc55a0..b69022a7a1e 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4940,6 +4940,11 @@ class TestCondition : public TestFixture { " if (j >= 0) {}\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f(int i) {\n" + " if ((128 + i < 255 ? 128 + i : 255) > 0) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void alwaysTrueContainer() { From 7d0a35cf5975a05b8b3c334738047f9a98e6b20f Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 5 Oct 2023 18:03:06 +0200 Subject: [PATCH 3/6] Undo --- test/testnullpointer.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 758d02c7b23..2c116825e21 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -2365,9 +2365,7 @@ class TestNullPointer : public TestFixture { " if (!flag1 && flag2)\n" " (*ptr)++;\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:10]: (warning) Either the condition 'ptr!=nullptr' is redundant or there is possible null pointer dereference: ptr.\n", - "", - errout.str()); + ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:10]: (warning) Either the condition 'ptr!=nullptr' is redundant or there is possible null pointer dereference: ptr.\n", errout.str()); } void nullpointer74() { From fcc68ea721fdc2451259caaa6b2a6864056791c1 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 5 Oct 2023 18:11:26 +0200 Subject: [PATCH 4/6] Comment [skip ci] --- test/testcondition.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index b69022a7a1e..bfe9713fce3 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4941,7 +4941,7 @@ class TestCondition : public TestFixture { "}\n"); ASSERT_EQUALS("", errout.str()); - check("void f(int i) {\n" + check("void f(int i) {\n" // #12039 " if ((128 + i < 255 ? 128 + i : 255) > 0) {}\n" "}\n"); ASSERT_EQUALS("", errout.str()); From ae8535cc223c06c75c3af0619734916d41216155 Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 5 Oct 2023 18:59:00 +0200 Subject: [PATCH 5/6] Run CI From 620344b2bd6f2e949c8401a639c6fb730d50272d Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Thu, 5 Oct 2023 23:08:55 +0200 Subject: [PATCH 6/6] Copy value --- lib/valueflow.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 13a8f0f324f..dec297dc5a6 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1857,8 +1857,7 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings* sett std::vector values; for (const Token* tok2 : tokens) { if (tok2->hasKnownIntValue()) { - values.emplace_back(); - values.back().intvalue = tok2->getKnownIntValue(); + values.emplace_back(tok2->values().front()); } else { ValueFlow::Value symValue{}; symValue.valueType = ValueFlow::Value::ValueType::SYMBOLIC;