Skip to content

Commit e1b5791

Browse files
Partial fix for #15210 FN uninitvar with brace init (#9021)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent ba89fed commit e1b5791

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎lib/astutils.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3432,14 +3432,15 @@ static ExprUsage getFunctionUsage(const Token* tok, int indirect, const Library&
34323432
if (!ftok)
34333433
return ExprUsage::None;
34343434
const Function* func = ftok->function();
3435+
const Variable* var = ftok->variable();
34353436
// variable init/constructor call?
3436-
if (!func && ftok->variable() && ftok == ftok->variable()->nameToken()) {
3437-
// STL types or containers don't initialize external variables
3438-
if (indirect == 0 && (ftok->variable()->isStlType() || (ftok->variable()->valueType() && ftok->variable()->valueType()->container)))
3437+
if (!func && var && ftok == var->nameToken()) {
3438+
// STL types/containers/primitive types don't initialize external variables
3439+
if (indirect == 0 && (var->isStlType() || (var->valueType() && (var->valueType()->container || (var->valueType()->isPrimitive() && !var->isReference())))))
34393440
return ExprUsage::Used;
34403441
// TODO: resolve multiple constructors
3441-
if (ftok->variable()->type() && ftok->variable()->type()->classScope) {
3442-
const int nCtor = ftok->variable()->type()->classScope->numConstructors;
3442+
if (var->type() && var->type()->classScope) {
3443+
const int nCtor = var->type()->classScope->numConstructors;
34433444
if (nCtor == 0) {
34443445
if (indirect > 0) {
34453446
const std::vector<const Variable*> argvar = getArgumentVars(ftok->astParent(), argnr);
@@ -3449,7 +3450,7 @@ static ExprUsage getFunctionUsage(const Token* tok, int indirect, const Library&
34493450
return ExprUsage::Used;
34503451
}
34513452
if (nCtor == 1) {
3452-
const Scope* scope = ftok->variable()->type()->classScope;
3453+
const Scope* scope = var->type()->classScope;
34533454
auto it = std::find_if(scope->functionList.begin(), scope->functionList.end(), [](const Function& f) {
34543455
return f.isConstructor();
34553456
});

‎test/testuninitvar.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6873,6 +6873,26 @@ class TestUninitVar : public TestFixture {
68736873
" return a[0];\n"
68746874
"}\n");
68756875
ASSERT_EQUALS("", errout_str());
6876+
6877+
valueFlowUninit("int f() {\n" // #15210
6878+
" int a;\n"
6879+
" int b{ a };\n"
6880+
" return b;\n"
6881+
"}\n"
6882+
"int g() {\n"
6883+
" int a;\n"
6884+
" int b(a);\n"
6885+
" return b;\n"
6886+
"}\n"
6887+
"int h() {\n"
6888+
" int a;\n"
6889+
" int& b{ a };\n"
6890+
" b = 0;\n"
6891+
" return b;\n"
6892+
"}\n");
6893+
ASSERT_EQUALS("[test.cpp:3:12]: (error) Uninitialized variable: a [uninitvar]\n"
6894+
"[test.cpp:8:11]: (error) Uninitialized variable: a [uninitvar]\n",
6895+
errout_str());
68766896
}
68776897

68786898
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value

0 commit comments

Comments
 (0)