Skip to content

Commit a4f9cb7

Browse files
committed
Uninitialized variables: Fixed false positives when passing allocated pointer to function
1 parent ef15e40 commit a4f9cb7

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,9 +1693,9 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
16931693
return true;
16941694
if (Token::Match(argStart, "const %type% & %var% [,)]"))
16951695
return true;
1696-
if (pointer && !address && Token::Match(argStart, "struct| %type% * %var% [,)]"))
1696+
if (pointer && !address && !alloc && Token::Match(argStart, "struct| %type% * %var% [,)]"))
16971697
return true;
1698-
if ((pointer || address) && Token::Match(argStart, "const struct| %type% * %var% [,)]"))
1698+
if ((pointer || address) && !alloc && Token::Match(argStart, "const struct| %type% * %var% [,)]"))
16991699
return true;
17001700
if ((pointer || address) && Token::Match(argStart, "const %type% %var% [") && Token::Match(argStart->linkAt(3), "] [,)]"))
17011701
return true;

test/testuninitvar.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,6 +3278,14 @@ class TestUninitVar : public TestFixture {
32783278
" return ab->a;\n" // error
32793279
"}");
32803280
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
3281+
3282+
checkUninitVar2("struct AB { int a; int b; };\n"
3283+
"void do_something(struct AB *ab);\n" // unknown function
3284+
"int f() {\n"
3285+
" struct AB *ab = malloc(sizeof(struct AB));\n"
3286+
" do_something(ab);\n"
3287+
"}");
3288+
ASSERT_EQUALS("", errout.str());
32813289
}
32823290

32833291
void syntax_error() { // Ticket #5073

0 commit comments

Comments
 (0)