Fix #12091 (False negative: Uninitialized variable read in subfunction (regression)) (#5739)

This commit is contained in:
Daniel Marjamäki 2023-12-08 21:54:23 +01:00 committed by GitHub
parent f5109df632
commit 1af83ad821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 11 deletions

View File

@ -7325,9 +7325,16 @@ struct MultiValueFlowAnalyzer : ValueFlowAnalyzer {
return false; return false;
} }
bool stopOnCondition(const Token* /*condTok*/) const override { bool stopOnCondition(const Token* condTok) const override {
// TODO fix false negatives if (isConditional())
return true; // isConditional(); return true;
if (!condTok->hasKnownIntValue() && values.count(condTok->varId()) == 0) {
const auto& values_ = condTok->values();
return std::any_of(values_.cbegin(), values_.cend(), [](const ValueFlow::Value& v) {
return v.isSymbolicValue() && Token::Match(v.tokvalue, "%oror%|&&");
});
}
return false;
} }
bool updateScope(const Token* endBlock, bool /*modified*/) const override { bool updateScope(const Token* endBlock, bool /*modified*/) const override {

View File

@ -2403,12 +2403,11 @@ private:
"void g(const std::vector<int>& w) {\n" "void g(const std::vector<int>& w) {\n"
" f(-1, w);\n" " f(-1, w);\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("test.cpp:5:warning:Array index -1 is out of bounds.\n" ASSERT_EQUALS("test.cpp:5:warning:Array index -1 is out of bounds.\n"
"test.cpp:8:note:Calling function 'f', 1st argument '-1' value is -1\n" "test.cpp:8:note:Calling function 'f', 1st argument '-1' value is -1\n"
"test.cpp:3:note:Assuming condition is false\n" "test.cpp:3:note:Assuming condition is false\n"
"test.cpp:5:note:Negative array index\n", "test.cpp:5:note:Negative array index\n",
"", errout.str());
errout.str());
settings = oldSettings; settings = oldSettings;
} }

View File

@ -6587,7 +6587,7 @@ private:
" bool copied_all = true;\n" " bool copied_all = true;\n"
" g(&copied_all, 5, 6, &bytesCopied);\n" " g(&copied_all, 5, 6, &bytesCopied);\n"
"}"); "}");
TODO_ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (warning) Uninitialized variable: *buflen\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (warning) Uninitialized variable: *buflen\n", errout.str());
// # 9953 // # 9953
valueFlowUninit("uint32_t f(uint8_t *mem) {\n" valueFlowUninit("uint32_t f(uint8_t *mem) {\n"

View File

@ -4511,7 +4511,7 @@ private:
"void f(Object *obj) {\n" "void f(Object *obj) {\n"
" if (valid(obj, K0)) {}\n" " if (valid(obj, K0)) {}\n"
"}\n"; "}\n";
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 7U, 0)); ASSERT_EQUALS(true, testValueOfX(code, 7U, 0));
ASSERT_EQUALS(false, testValueOfXKnown(code, 7U, 0)); ASSERT_EQUALS(false, testValueOfXKnown(code, 7U, 0));
code = "int f(int i) {\n" code = "int f(int i) {\n"
@ -5624,6 +5624,17 @@ private:
"}\n"; "}\n";
values = tokenValues(code, "x <", ValueFlow::Value::ValueType::UNINIT); values = tokenValues(code, "x <", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size()); ASSERT_EQUALS(0, values.size());
code = "void g(bool *result, size_t *buflen) {\n" // #12091
" if (*result && *buflen >= 5) {}\n" // <- *buflen might not be initialized
"}\n"
"void f() {\n"
" size_t bytesCopied;\n"
" bool copied_all = true;\n"
" g(&copied_all, &bytesCopied);\n"
"}";
values = tokenValues(code, "buflen >=", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(1, values.size());
} }
void valueFlowConditionExpressions() { void valueFlowConditionExpressions() {