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 12461: False positive: uninitialized buffer, address of and function call #6248

Merged
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
9 changes: 7 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,13 @@ const Token* getParentLifetime(const Token* tok, const Library* library)
std::vector<const Token*> members = getParentMembers(tok);
if (members.size() < 2)
return tok;
// Find the first local variable or temporary
// Find the first local variable, temporary, or array
auto it = std::find_if(members.crbegin(), members.crend(), [&](const Token* tok2) {
const Variable* var = tok2->variable();
if (var)
return var->isLocal() || var->isArgument();
if (Token::simpleMatch(tok2, "["))
return true;
return isTemporary(tok2, library);
});
if (it == members.rend())
Expand Down Expand Up @@ -668,7 +670,10 @@ const Token* getParentLifetime(const Token* tok, const Library* library)
return var && var->isReference();
}))
return nullptr;
return *it;
const Token* result = *it;
if (Token::simpleMatch(result, "[") && result->astOperand1())
return getParentLifetime(result->astOperand1());
return result;
}

static bool isInConstructorList(const Token* tok)
Expand Down
25 changes: 25 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7445,6 +7445,31 @@ class TestUninitVar : public TestFixture {
" p->f();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:9]: (warning) Uninitialized variable: p\n", errout_str());

// #12461
valueFlowUninit("struct stry_type {\n"
" void *out;\n"
"};\n"
"void bar(str_type *items);\n"
"void foo() {\n"
" str_type st_arr[1];\n"
" char arr[5];\n"
" st_arr[0].out = &arr;\n"
" bar(st_arr);\n"
" int len = strlen(arr);\n"
"}\n");
ASSERT_EQUALS("", errout_str());

valueFlowUninit("struct stry_type {\n"
" void *out;\n"
"};\n"
"void foo() {\n"
" str_type st_arr[1];\n"
" char arr[5];\n"
" st_arr[0].out = &arr;\n"
" int len = strlen(arr);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: arr\n", errout_str());
}

void uninitvar_memberfunction() {
Expand Down
Loading