Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12685 FN uninitvar for constructor argument #6368

Merged
merged 5 commits into from
May 2, 2024
Merged
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
32 changes: 24 additions & 8 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3295,15 +3295,36 @@ static ExprUsage getFunctionUsage(const Token* tok, int indirect, const Settings
const Token* ftok = getTokenArgumentFunction(tok, argnr);
if (!ftok)
return ExprUsage::None;
if (ftok->function()) {
const Function* func = ftok->function();
// variable init/constructor call?
if (!func && ftok->variable() && ftok == ftok->variable()->nameToken()) {
// STL types or containers don't initialize external variables
if (ftok->variable()->isStlType() || (ftok->variable()->valueType() && ftok->variable()->valueType()->container))
return ExprUsage::Used;
// TODO: resolve multiple constructors
if (ftok->variable()->type() && ftok->variable()->type()->classScope) {
const int nCtor = ftok->variable()->type()->classScope->numConstructors;
if (nCtor == 0)
return ExprUsage::Used;
if (nCtor == 1) {
const Scope* scope = ftok->variable()->type()->classScope;
auto it = std::find_if(scope->functionList.begin(), scope->functionList.end(), [](const Function& f) {
return f.isConstructor();
});
if (it != scope->functionList.end())
func = &*it;
}
}
}
if (func) {
std::vector<const Variable*> args = getArgumentVars(ftok, argnr);
for (const Variable* arg : args) {
if (!arg)
continue;
if (arg->isReference() || (arg->isPointer() && indirect == 1)) {
if (!ftok->function()->hasBody())
if (!func->hasBody())
return ExprUsage::PassedByReference;
for (const Token* bodytok = ftok->function()->functionScope->bodyStart; bodytok != ftok->function()->functionScope->bodyEnd; bodytok = bodytok->next()) {
for (const Token* bodytok = func->functionScope->bodyStart; bodytok != func->functionScope->bodyEnd; bodytok = bodytok->next()) {
if (bodytok->variable() == arg) {
if (arg->isReference())
return ExprUsage::PassedByReference;
Expand All @@ -3321,11 +3342,6 @@ 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()->classScope && ftok->variable()->type()->classScope->numConstructors == 0)
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
22 changes: 21 additions & 1 deletion test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7438,7 +7438,7 @@ class TestUninitVar : public TestFixture {
"[test.cpp:27]: (error) Uninitialized variable: s.t.j\n",
errout_str());

valueFlowUninit("struct S { int x; };\n"
valueFlowUninit("struct S { int x; };\n" // #6933
"void f() {\n"
" int i;\n"
" S s(i);\n"
Expand Down Expand Up @@ -7525,6 +7525,26 @@ class TestUninitVar : public TestFixture {
" return s2;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

valueFlowUninit("struct S {\n" // #12685
" explicit S(double v);\n"
" double m;\n"
"};\n"
"void f() {\n"
" double d;\n"
" S s(d);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: d\n", errout_str());

valueFlowUninit("struct S {\n"
" explicit S(double v) : m(v) {}\n"
" double m;\n"
"};\n"
"void f() {\n"
" double d;\n"
" S s{ d };\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: d\n", errout_str());
}

void uninitvar_memberfunction() {
Expand Down
Loading