Skip to content

Commit

Permalink
Partial fix for #12922 FN uninitvar with std::array::at() (#6589)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Jul 12, 2024
1 parent 818dbee commit 7aa5e4a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7077,6 +7077,15 @@ static bool needsInitialization(const Variable* var)
return true;
if (var->valueType()->type == ValueType::Type::ITERATOR)
return true;
if (var->isStlType() && var->isArray()) {
if (const Token* ctt = var->valueType()->containerTypeToken) {
if (ctt->isStandardType())
return true;
const Type* ct = ctt->type();
if (ct && ct->needInitialization == Type::NeedInitialization::True)
return true;
}
}
}
return false;
}
Expand Down
22 changes: 22 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6520,6 +6520,28 @@ class TestUninitVar : public TestFixture {
" printf(\"%s\", p);\n"
"}\n");
ASSERT_EQUALS("", errout_str());

valueFlowUninit("std::string f() {\n" // #12922
" std::string a[2];\n"
" std::array<std::string, 2> b;\n"
" return a[1] + b.at(1);\n" // don't warn
"}\n"
"struct S { int i; };\n"
"struct T { int i = 0; };\n"
"int g() {\n"
" std::array<int, 2> a;\n"
" std::array<S, 2> b;\n"
" std::array<T, 2> c;\n" // don't warn
" return a.at(1) + b.at(1).i + c.at(1).i;\n"
"}\n"
"int h() {\n"
" std::array<int, 2> a;\n"
" return a[1];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:12]: (error) Uninitialized variable: a\n"
"[test.cpp:12]: (error) Uninitialized variable: b\n"
"[test.cpp:16]: (error) Uninitialized variable: a\n",
errout_str());
}

void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
Expand Down
2 changes: 1 addition & 1 deletion test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6304,7 +6304,7 @@ class TestValueFlow : public TestFixture {
" std::array<int,10> ints;\n" // Array size is 10
" ints.front();\n"
"}";
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 10));
ASSERT_EQUALS("values.size():2", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 10)); // uninit value

code = "void f() {\n"
" std::string s;\n"
Expand Down

0 comments on commit 7aa5e4a

Please sign in to comment.