Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ void CheckUninitVarImpl::uninitvarError(const Token* tok, const ValueFlow::Value
const std::string& varname = ltok ? ltok->expressionString() : "x";
ErrorPath errorPath = v.errorPath;
errorPath.emplace_back(tok, "");
auto severity = v.isKnown() ? Severity::error : Severity::warning;
auto severity = v.isKnown() && !v.conditional ? Severity::error : Severity::warning;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

conditional is not a reliable sign that the read is only possibly uninitialized. Here it gets set depending on what follows the if.

  • int i; if (b) return i; return 0; still gives error.
  • int i; if (b) return i; i = 1; return i; now gives warning.
  • if (b) g(i); i = 1; g(i); is also demoted to warning.

In each case the read inside the branch is uninitialized every time it executes, so these are definite bugs that used to be errors. The patch makes the severity less consistent instead of more.

auto certainty = v.isInconclusive() ? Certainty::inconclusive : Certainty::normal;
if (v.subexpressions.empty()) {
reportError(std::move(errorPath),
Expand Down
18 changes: 18 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class TestUninitVar : public TestFixture {
TEST_CASE(valueFlowUninitBreak);
TEST_CASE(valueFlowUninitStructMembers);
TEST_CASE(valueFlowUninitForLoop);
TEST_CASE(valueFlowUninitErrorPath);
TEST_CASE(uninitvar_ipa);
TEST_CASE(uninitvar_memberfunction);
TEST_CASE(uninitvar_nonmember); // crash in ycmd test
Expand Down Expand Up @@ -7945,6 +7946,23 @@ class TestUninitVar : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void valueFlowUninitErrorPath()
{
setMultiline();

valueFlowUninit("void f(bool b) {\n"
" int i;\n"
" if (b)\n"
" return i;\n"
" i = 1;\n"
" return i;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:16]: warning: Uninitialized variable: i [uninitvar]\n"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This expectation locks in a downgrade: return i; inside if (b) always reads an uninitialized i, and base reports it as an error. If return 0; replaces the i = 1; return i; tail, the same read is still reported as an error, so this test documents the inconsistency instead of fixing it.

"[test.cpp:3:9]: note: Assuming condition is true\n"
"[test.cpp:4:16]: note: Uninitialized variable: i\n",
errout_str());
}

void uninitvar_memberfunction() {
// # 8715
valueFlowUninit("struct C {\n"
Expand Down
Loading