Skip to content

Commit c0793bb

Browse files
Partial fix for #14640 FP uninitvar (address in init list) (danmar#8407)
1 parent 09c1161 commit c0793bb

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

lib/astutils.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3422,8 +3422,14 @@ static ExprUsage getFunctionUsage(const Token* tok, int indirect, const Settings
34223422
// TODO: resolve multiple constructors
34233423
if (ftok->variable()->type() && ftok->variable()->type()->classScope) {
34243424
const int nCtor = ftok->variable()->type()->classScope->numConstructors;
3425-
if (nCtor == 0)
3425+
if (nCtor == 0) {
3426+
if (indirect > 0) {
3427+
const std::vector<const Variable*> argvar = getArgumentVars(ftok->astParent(), argnr);
3428+
if (argvar.size() == 1 && argvar[0]->valueType() && argvar[0]->valueType()->pointer == indirect)
3429+
return ExprUsage::NotUsed;
3430+
}
34263431
return ExprUsage::Used;
3432+
}
34273433
if (nCtor == 1) {
34283434
const Scope* scope = ftok->variable()->type()->classScope;
34293435
auto it = std::find_if(scope->functionList.begin(), scope->functionList.end(), [](const Function& f) {

lib/token.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,6 +2356,8 @@ const ::Type* Token::typeOf(const Token* tok, const Token** typeTok)
23562356
return tok->variable()->type();
23572357
if (tok->function())
23582358
return tok->function()->retType;
2359+
if (tok->valueType() && tok->valueType()->typeScope && tok->valueType()->typeScope->definedType)
2360+
return tok->valueType()->typeScope->definedType;
23592361
if (Token::simpleMatch(tok, "return")) {
23602362
// cppcheck-suppress shadowFunction - TODO: fix this
23612363
const Scope *scope = tok->scope();

test/testuninitvar.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7776,6 +7776,14 @@ class TestUninitVar : public TestFixture {
77767776
" return (&s)->y;\n"
77777777
"}\n");
77787778
ASSERT_EQUALS("[test.cpp:5:16]: (error) Uninitialized variable: s.y [uninitvar]\n", errout_str());
7779+
7780+
valueFlowUninit("struct S { int* p; };\n" // #14640
7781+
"void f() {\n"
7782+
" int x;\n"
7783+
" S s{ &x };\n"
7784+
" *s.p = 0;\n"
7785+
"}\n");
7786+
ASSERT_EQUALS("", errout_str());
77797787
}
77807788

77817789
void valueFlowUninitForLoop()

0 commit comments

Comments
 (0)