Skip to content

Commit 6182394

Browse files
committed
Uninitialized variables: Fixed false positive taking value of pointer that is allocated but not initialized
1 parent eb2ea1c commit 6182394

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
@@ -1757,7 +1757,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
17571757
}
17581758
}
17591759

1760-
if (Token::Match(vartok->previous(), "= %var% ;|%cop%"))
1760+
if (!alloc && Token::Match(vartok->previous(), "= %var% ;|%cop%"))
17611761
return true;
17621762

17631763
if (Token::Match(vartok->previous(), "? %var%")) {

test/testuninitvar.cpp

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

3351+
checkUninitVar2("void f() {\n"
3352+
" char *p = malloc(100);\n"
3353+
" x = p;\n"
3354+
"}\n");
3355+
ASSERT_EQUALS("", errout.str());
3356+
33513357
// function parameter (treat it as initialized until malloc is used)
33523358
checkUninitVar2("int f(int *p) {\n"
33533359
" if (*p == 1) {}\n" // no error

0 commit comments

Comments
 (0)