Skip to content

Commit 18373bc

Browse files
authored
Fix 12116: FP negativeContainerIndex with redundant assignment (regression) (#5602)
1 parent 56bfa9f commit 18373bc

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

lib/valueflow.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5643,12 +5643,15 @@ static void valueFlowForwardConst(Token* start,
56435643
if (v.tokvalue->varId() != var->declarationId())
56445644
continue;
56455645
for (ValueFlow::Value value : values) {
5646+
if (!v.isKnown() && value.isImpossible())
5647+
continue;
56465648
if (v.intvalue != 0) {
56475649
if (!value.isIntValue())
56485650
continue;
56495651
value.intvalue += v.intvalue;
56505652
}
5651-
value.valueKind = v.valueKind;
5653+
if (!value.isImpossible())
5654+
value.valueKind = v.valueKind;
56525655
value.bound = v.bound;
56535656
value.errorPath.insert(value.errorPath.end(), v.errorPath.cbegin(), v.errorPath.cend());
56545657
setTokenValue(tok, std::move(value), settings);

test/testcondition.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4722,6 +4722,16 @@ class TestCondition : public TestFixture {
47224722
" (it != end) && *it;\n"
47234723
"}\n");
47244724
ASSERT_EQUALS("", errout.str());
4725+
4726+
// #12116
4727+
check("void f(int n) {\n"
4728+
" for (int i = 0; i < N; ++i) {\n"
4729+
" if (i < n) {}\n"
4730+
" else if (i > n) {}\n"
4731+
" else {}\n"
4732+
" }\n"
4733+
"}\n");
4734+
ASSERT_EQUALS("", errout.str());
47254735
}
47264736

47274737
void alwaysTrueInfer() {

test/testvalueflow.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8174,6 +8174,18 @@ class TestValueFlow : public TestFixture {
81748174
" }\n"
81758175
"}\n";
81768176
ASSERT_EQUALS(true, testValueOfXImpossible(code, 4U, -1));
8177+
8178+
code = "void f(int N, int z) {\n"
8179+
" std::vector<int> a(N);\n"
8180+
" int m = -1;\n"
8181+
" m = 0;\n"
8182+
" for (int k = 0; k < N; k++) {\n"
8183+
" int x = m + k;\n"
8184+
" if (z == a[x]) {}\n"
8185+
" }\n"
8186+
"}\n";
8187+
ASSERT_EQUALS(true, testValueOfXImpossible(code, 7U, -1));
8188+
ASSERT_EQUALS(false, testValueOfXKnown(code, 7U, -1));
81778189
}
81788190

81798191
void valueFlowImpossibleUnknownConstant()

0 commit comments

Comments
 (0)