Fix #6933 FN uninitvar with POD struct and STL types (#5713)

Co-authored-by: chrchr-github <chrchr@github>
This commit is contained in:
chrchr-github 2023-12-01 17:42:27 +01:00 committed by GitHub
parent 3272a2bbe7
commit 55c2b75c2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 1 deletions

View File

@ -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)

View File

@ -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) {

View File

@ -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);
}

View File

@ -7251,6 +7251,24 @@ private:
"[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() {