From da5478fb23ca14ba4fe077aa01866ab0375d4188 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 24 Mar 2026 14:17:01 +0100 Subject: [PATCH 01/10] Update vf_settokenvalue.cpp --- lib/vf_settokenvalue.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vf_settokenvalue.cpp b/lib/vf_settokenvalue.cpp index 12acc01dedd..d3a22769624 100644 --- a/lib/vf_settokenvalue.cpp +++ b/lib/vf_settokenvalue.cpp @@ -72,7 +72,7 @@ namespace ValueFlow return value; if (!parent->isBinaryOp()) return value; - if (!parent->isConstOp()) + if (!parent->isConstOp() && !parent->isAssignmentOp()) return value; if (!astIsIntegral(parent->astOperand1(), false)) return value; @@ -88,7 +88,7 @@ namespace ValueFlow ValueType::Sign sign = ValueType::Sign::UNSIGNED; if (n1 < n2) sign = vt2->sign; - else if (n1 > n2) + else if (n1 >= n2) sign = vt1->sign; Value v = castValue(value, sign, std::max(n1, n2) * 8); v.wideintvalue = value.intvalue; From c39ac62278da72647f92d6ec1777f5ca9ee4d3ef Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 24 Mar 2026 14:17:49 +0100 Subject: [PATCH 02/10] Update checkcondition.cpp --- lib/checkcondition.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 14fa900944d..e46b1be7533 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1975,8 +1975,6 @@ void CheckCondition::checkCompareValueOutOfTypeRange() continue; if (valueTok->getKnownIntValue() < 0 && valueTok->valueType() && valueTok->valueType()->sign != ValueType::Sign::SIGNED) continue; - if (valueTok->valueType() && valueTok->valueType()->isTypeEqual(typeTok->valueType())) - continue; std::uint8_t bits = 0; switch (typeTok->valueType()->type) { case ValueType::Type::BOOL: From 5ec9f9e50b668e492b9eb858075bdf97388739b1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 24 Mar 2026 14:31:44 +0100 Subject: [PATCH 03/10] Update testcondition.cpp --- test/testcondition.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 241d0400099..ec28c089bae 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4065,7 +4065,7 @@ class TestCondition : public TestFixture { " unsigned int num = max - 1;\n" " if (num < 0) {}\n" // <- do not report knownConditionTrueFalse "}"); - ASSERT_EQUALS("", errout_str()); + ASSERT_EQUALS("[test.cpp:3:15]: (style) Comparing expression of type 'unsigned int' against value 0. Condition is always false. [compareValueOutOfTypeRangeError]", errout_str()); // #10297 check("void foo(size_t len, int start) {\n" @@ -6355,6 +6355,15 @@ class TestCondition : public TestFixture { "}\n", settingsUnix64); ASSERT_EQUALS("[test.cpp:2:14]: (style) Comparing expression of type 'bool' against value 2. Condition is always true. [compareValueOutOfTypeRangeError]\n", errout_str()); + + check("void f(const std::uint32_t& u) {\n" // #9078 + " if (u >= UINT32_MAX) {}\n" + " if (u <= UINT32_MAX) {}\n" + " if (u > UINT32_MAX) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3:14]: (style) Comparing expression of type 'const unsigned int &' against value 4294967295. Condition is always true. [compareValueOutOfTypeRangeError]\n" + "[test.cpp:4:13]: (style) Comparing expression of type 'const unsigned int &' against value 4294967295. Condition is always false. [compareValueOutOfTypeRangeError]\n", + errout_str()); } void knownConditionCast() { From 6021d984e602a2244ca21d3cd8b818a9acad05fa Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 24 Mar 2026 14:42:36 +0100 Subject: [PATCH 04/10] Update testcondition.cpp --- test/testcondition.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index ec28c089bae..4a331b03030 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4065,7 +4065,7 @@ class TestCondition : public TestFixture { " unsigned int num = max - 1;\n" " if (num < 0) {}\n" // <- do not report knownConditionTrueFalse "}"); - ASSERT_EQUALS("[test.cpp:3:15]: (style) Comparing expression of type 'unsigned int' against value 0. Condition is always false. [compareValueOutOfTypeRangeError]", errout_str()); + ASSERT_EQUALS("[test.cpp:3:15]: (style) Comparing expression of type 'unsigned int' against value 0. Condition is always false. [compareValueOutOfTypeRangeError]\n", errout_str()); // #10297 check("void foo(size_t len, int start) {\n" From 483fd5141f61f6f934f20fff3743516489dca531 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 24 Mar 2026 15:21:07 +0100 Subject: [PATCH 05/10] Update testother.cpp --- test/testother.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index d2f57bbb2a4..be7629c1e89 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -9313,6 +9313,24 @@ class TestOther : public TestFixture { " for (unsigned p = 0; p < (sizeof(a) / sizeof((a)[0])); ++p) {}\n" "}"); ASSERT_EQUALS("", errout_str()); + + check("void f(const unsigned char u) {\n" + " if (u > 0) {}\n" + " if (u < 0) {}\n" + " if (u >= 0) {}\n" + " if (u <= 0) {}\n" + " if (0 < u) {}\n" + " if (0 > u) {}\n" + " if (0 <= u) {}\n" + " if (0 >= u) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:3:11]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n" + "[test.cpp:4:11]: (style) Unsigned expression 'u' can't be negative so it is unnecessary to test it. [unsignedPositive]\n" + "[test.cpp:5:11]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n" + "[test.cpp:7:13]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n" + "[test.cpp:8:13]: (style) Unsigned expression 'u' can't be negative so it is unnecessary to test it. [unsignedPositive]\n" + "[test.cpp:9:13]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n", + errout_str()); } void checkSignOfPointer() { From e3644543a26e4e385324347f9ab33b5264803455 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 24 Mar 2026 15:22:48 +0100 Subject: [PATCH 06/10] Update checkcondition.cpp --- lib/checkcondition.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index e46b1be7533..cf54c882f34 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -2013,6 +2013,8 @@ void CheckCondition::checkCompareValueOutOfTypeRange() bool result{}; const auto kiv = valueTok->getKnownIntValue(); + if (kiv == 0) + continue; // prevent overlap with TestOther::unsignedPositive/unsignedLessThanZero if (tok->str() == "==") result = false; else if (tok->str() == "!=") From cd821f3ee6a4423e477a82fa46c2773c73740a07 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 25 Mar 2026 08:23:37 +0100 Subject: [PATCH 07/10] Update testcondition.cpp [skip ci] --- test/testcondition.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 4a331b03030..29d63562fa3 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4065,7 +4065,7 @@ class TestCondition : public TestFixture { " unsigned int num = max - 1;\n" " if (num < 0) {}\n" // <- do not report knownConditionTrueFalse "}"); - ASSERT_EQUALS("[test.cpp:3:15]: (style) Comparing expression of type 'unsigned int' against value 0. Condition is always false. [compareValueOutOfTypeRangeError]\n", errout_str()); + ASSERT_EQUALS("", errout_str()); // #10297 check("void foo(size_t len, int start) {\n" @@ -6324,28 +6324,24 @@ class TestCondition : public TestFixture { check("void f(const unsigned char u) {\n" " if (u > 0) {}\n" - " if (u < 0) {}\n" // warn - " if (u >= 0) {}\n" // warn + " if (u < 0) {}\n" + " if (u >= 0) {}\n" " if (u <= 0) {}\n" " if (u > 255) {}\n" // warn " if (u < 255) {}\n" " if (u >= 255) {}\n" " if (u <= 255) {}\n" // warn " if (0 < u) {}\n" - " if (0 > u) {}\n" // warn - " if (0 <= u) {}\n" // warn + " if (0 > u) {}\n" + " if (0 <= u) {}\n" " if (0 >= u) {}\n" " if (255 < u) {}\n" // warn " if (255 > u) {}\n" " if (255 <= u) {}\n" " if (255 >= u) {}\n" // warn "}\n", settingsUnix64); - ASSERT_EQUALS("[test.cpp:3:14]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always false. [compareValueOutOfTypeRangeError]\n" - "[test.cpp:4:14]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always true. [compareValueOutOfTypeRangeError]\n" - "[test.cpp:6:14]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always false. [compareValueOutOfTypeRangeError]\n" + ASSERT_EQUALS("[test.cpp:6:14]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always false. [compareValueOutOfTypeRangeError]\n" "[test.cpp:9:14]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always true. [compareValueOutOfTypeRangeError]\n" - "[test.cpp:11:9]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always false. [compareValueOutOfTypeRangeError]\n" - "[test.cpp:12:9]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always true. [compareValueOutOfTypeRangeError]\n" "[test.cpp:14:9]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always false. [compareValueOutOfTypeRangeError]\n" "[test.cpp:17:9]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always true. [compareValueOutOfTypeRangeError]\n", errout_str()); From f4a5eb941bc5a72581116c336c2c88d9330255f3 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 25 Mar 2026 08:24:29 +0100 Subject: [PATCH 08/10] Update testother.cpp [skip ci] --- test/testother.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index be7629c1e89..00d25fc1b22 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -9327,9 +9327,9 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:3:11]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n" "[test.cpp:4:11]: (style) Unsigned expression 'u' can't be negative so it is unnecessary to test it. [unsignedPositive]\n" "[test.cpp:5:11]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n" - "[test.cpp:7:13]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n" - "[test.cpp:8:13]: (style) Unsigned expression 'u' can't be negative so it is unnecessary to test it. [unsignedPositive]\n" - "[test.cpp:9:13]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n", + "[test.cpp:7:11]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n" + "[test.cpp:8:11]: (style) Unsigned expression 'u' can't be negative so it is unnecessary to test it. [unsignedPositive]\n" + "[test.cpp:9:11]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n", errout_str()); } From 1eb3825b11d542419ba31f0b9597dee8a09fe44a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 25 Mar 2026 08:31:39 +0100 Subject: [PATCH 09/10] Update testvalueflow.cpp --- test/testvalueflow.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 709be2a204f..265704f6844 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -3112,6 +3112,12 @@ class TestValueFlow : public TestFixture { " return x;\n" "}\n"; ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 1)); + + code = "int32_t f() {\n" + " const int32_t x = 0xffffffff;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfXKnown(code, 3U, -1)); } void valueFlowAfterSwap() From 1d7256a24e42b451839adbf3de32244ab63fd479 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 25 Mar 2026 09:31:35 +0100 Subject: [PATCH 10/10] Update vf_settokenvalue.cpp --- lib/vf_settokenvalue.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vf_settokenvalue.cpp b/lib/vf_settokenvalue.cpp index d3a22769624..602d87e159d 100644 --- a/lib/vf_settokenvalue.cpp +++ b/lib/vf_settokenvalue.cpp @@ -88,7 +88,7 @@ namespace ValueFlow ValueType::Sign sign = ValueType::Sign::UNSIGNED; if (n1 < n2) sign = vt2->sign; - else if (n1 >= n2) + else // (n1 >= n2) sign = vt1->sign; Value v = castValue(value, sign, std::max(n1, n2) * 8); v.wideintvalue = value.intvalue;