Skip to content

Commit 26dfdda

Browse files
committed
UninitVar: Fixed false positive when checking if allocated pointer is null
1 parent 4c44e62 commit 26dfdda

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

lib/checkuninitvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
18121812
return (var && var->typeStartToken()->isStandardType());
18131813
}
18141814

1815-
if (vartok->next() && vartok->next()->isOp() && !vartok->next()->isAssignmentOp())
1815+
if (!alloc && vartok->next() && vartok->next()->isOp() && !vartok->next()->isAssignmentOp())
18161816
return true;
18171817

18181818
if (vartok->strAt(1) == "]")

test/testuninitvar.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3342,6 +3342,12 @@ class TestUninitVar : public TestFixture {
33423342
"}\n");
33433343
ASSERT_EQUALS("", errout.str());
33443344

3345+
checkUninitVar2("void f() {\n"
3346+
" char *p = malloc(100);\n"
3347+
" p || assert_failed();\n"
3348+
"}\n");
3349+
ASSERT_EQUALS("", errout.str());
3350+
33453351
// function parameter (treat it as initialized until malloc is used)
33463352
checkUninitVar2("int f(int *p) {\n"
33473353
" if (*p == 1) {}\n" // no error

0 commit comments

Comments
 (0)