Fix 12116: FP negativeContainerIndex with redundant assignment (regression) (#5602)

This commit is contained in:
Paul Fultz II 2023-10-28 17:11:03 -05:00 committed by GitHub
parent 56bfa9f3ea
commit 18373bc64b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 1 deletions

View File

@ -5643,12 +5643,15 @@ static void valueFlowForwardConst(Token* start,
if (v.tokvalue->varId() != var->declarationId())
continue;
for (ValueFlow::Value value : values) {
if (!v.isKnown() && value.isImpossible())
continue;
if (v.intvalue != 0) {
if (!value.isIntValue())
continue;
value.intvalue += v.intvalue;
}
value.valueKind = v.valueKind;
if (!value.isImpossible())
value.valueKind = v.valueKind;
value.bound = v.bound;
value.errorPath.insert(value.errorPath.end(), v.errorPath.cbegin(), v.errorPath.cend());
setTokenValue(tok, std::move(value), settings);

View File

@ -4722,6 +4722,16 @@ private:
" (it != end) && *it;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// #12116
check("void f(int n) {\n"
" for (int i = 0; i < N; ++i) {\n"
" if (i < n) {}\n"
" else if (i > n) {}\n"
" else {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void alwaysTrueInfer() {

View File

@ -8174,6 +8174,18 @@ private:
" }\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXImpossible(code, 4U, -1));
code = "void f(int N, int z) {\n"
" std::vector<int> a(N);\n"
" int m = -1;\n"
" m = 0;\n"
" for (int k = 0; k < N; k++) {\n"
" int x = m + k;\n"
" if (z == a[x]) {}\n"
" }\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXImpossible(code, 7U, -1));
ASSERT_EQUALS(false, testValueOfXKnown(code, 7U, -1));
}
void valueFlowImpossibleUnknownConstant()