Uninitialized variables: Fixed false positive for 'p=malloc; if (p) {}'

This commit is contained in:
Daniel Marjamäki 2013-12-12 11:44:07 +01:00
parent 4fbe15c866
commit 1b86615a69
2 changed files with 8 additions and 2 deletions

View File

@ -1703,7 +1703,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
} else if (Token::Match(start->previous(), "if|while|for")) { } else if (Token::Match(start->previous(), "if|while|for")) {
// control-flow statement reading the variable "by value" // control-flow statement reading the variable "by value"
return true; return !alloc;
} }
} }
} }
@ -1766,7 +1766,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
bool unknown = false; bool unknown = false;
if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) { if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) {
// pointer is allocated // pointer is allocated - dereferencing it is ok.
if (alloc) if (alloc)
return false; return false;

View File

@ -3272,6 +3272,12 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar2("void f() {\n"
" char *s = malloc(100);\n"
" if (s != NULL) { }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// function parameter (treat it as initialized until malloc is used) // function parameter (treat it as initialized until malloc is used)
checkUninitVar2("int f(int *p) {\n" checkUninitVar2("int f(int *p) {\n"
" if (*p == 1) {}\n" // no error " if (*p == 1) {}\n" // no error