Skip to content

Commit

Permalink
Fix danmar#6933 FN uninitvar with POD struct and STL types (danmar#5713)
Browse files Browse the repository at this point in the history
Co-authored-by: chrchr-github <chrchr@github>
  • Loading branch information
chrchr-github and chrchr-github authored Dec 1, 2023
1 parent 3272a2b commit 55c2b75
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3257,6 +3257,11 @@ static ExprUsage getFunctionUsage(const Token* tok, int indirect, const Settings
return ExprUsage::Used;
} else if (ftok->str() == "{") {
return indirect == 0 ? ExprUsage::Used : ExprUsage::Inconclusive;
} else if (ftok->variable() && ftok == ftok->variable()->nameToken()) { // variable init/constructor call
if (ftok->variable()->type() && ftok->variable()->type()->needInitialization == Type::NeedInitialization::True)
return ExprUsage::Used;
if (ftok->variable()->isStlType() || (ftok->variable()->valueType() && ftok->variable()->valueType()->container)) // STL types or containers don't initialize external variables
return ExprUsage::Used;
} else {
const bool isnullbad = settings->library.isnullargbad(ftok, argnr + 1);
if (indirect == 0 && astIsPointer(tok) && !addressOf && isnullbad)
Expand Down
7 changes: 7 additions & 0 deletions test/cfg/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ void duplicateExpression_QString_Compare(QString style) //#8723
{}
}

void QVector_uninit()
{
int i;
// cppcheck-suppress [uninitvar, unreadVariable]
QVector<int> v(i);
}

void QStack1(QStack<int> intStackArg)
{
for (int i = 0; i <= intStackArg.size(); ++i) {
Expand Down
2 changes: 1 addition & 1 deletion test/cfg/std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,8 +1043,8 @@ void uninitvar_isxdigit(void)
void uninitvar_proj(void)
{
double d;
// cppcheck-suppress uninitvar
const std::complex<double> dc(d,d);
// TODO cppcheck-suppress uninitvar
(void)std::proj(dc);
}

Expand Down
18 changes: 18 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7251,6 +7251,24 @@ class TestUninitVar : public TestFixture {
"[test.cpp:23]: (error) Uninitialized variable: s.t.j\n"
"[test.cpp:27]: (error) Uninitialized variable: s.t.j\n",
errout.str());

valueFlowUninit("struct S { int x; };\n"
"void f() {\n"
" int i;\n"
" S s(i);\n"
"}\n"
"void g() {\n"
" int i;\n"
" S t{ i };\n"
"}\n"
"void h() {\n"
" int i;\n"
" std::vector<int> v(i);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n"
"[test.cpp:8]: (error) Uninitialized variable: i\n"
"[test.cpp:12]: (error) Uninitialized variable: i\n",
errout.str());
}

void uninitvar_memberfunction() {
Expand Down

0 comments on commit 55c2b75

Please sign in to comment.