Uninitialized variables: Fixed false positive

This commit is contained in:
Daniel Marjamäki 2019-03-09 11:30:45 +01:00
parent 5915fd50a5
commit b9ac77a31b
2 changed files with 6 additions and 20 deletions

View File

@ -925,20 +925,8 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al
// Accessing Rvalue member using "." or "->" // Accessing Rvalue member using "." or "->"
if (Token::Match(vartok->previous(), "!!& %var% .")) { if (Token::Match(vartok->previous(), "!!& %var% .")) {
// Is struct member passed to function? // Is struct member passed to function?
if (!pointer) { if (!pointer)
// TODO: there are FN currently: return false;
// - should only return false if struct member is (or might be) array.
// - should only return false if function argument is (or might be) non-const pointer or reference
bool unknown = false;
const Token *possibleParent = getAstParentSkipPossibleCastAndAddressOf(vartok, &unknown);
if (Token::Match(possibleParent, "[(,]")) {
if (unknown)
return false; // TODO: output some info message?
const int use = isFunctionParUsage(vartok, pointer, alloc);
if (use >= 0)
return (use>0);
}
}
if (pointer && alloc != CTOR_CALL && Token::Match(vartok, "%name% . %name% (")) if (pointer && alloc != CTOR_CALL && Token::Match(vartok, "%name% . %name% ("))
return true; return true;

View File

@ -3054,8 +3054,7 @@ private:
" struct AB ab;\n" " struct AB ab;\n"
" do_something(ab.a);\n" " do_something(ab.a);\n"
"}\n", "test.c"); "}\n", "test.c");
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized variable: ab\n" ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
"[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar("struct AB { int a; int b; };\n" checkUninitVar("struct AB { int a; int b; };\n"
"void do_something(const struct AB &ab) { a = ab.a; }\n" "void do_something(const struct AB &ab) { a = ab.a; }\n"
@ -3235,14 +3234,14 @@ private:
" struct AB ab;\n" " struct AB ab;\n"
" strcpy(x, ab.a);\n" " strcpy(x, ab.a);\n"
"}\n", "test.c"); "}\n", "test.c");
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab\n", errout.str()); TODO_ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab.a\n", "", errout.str());
checkUninitVar("struct AB { int a; };\n" checkUninitVar("struct AB { int a; };\n"
"void f(void) {\n" "void f(void) {\n"
" struct AB ab;\n" " struct AB ab;\n"
" dosomething(ab.a);\n" " dosomething(ab.a);\n"
"}\n", "test.c"); "}\n", "test.c");
TODO_ASSERT_EQUALS("error","", errout.str()); ASSERT_EQUALS("", errout.str());
} }
checkUninitVar("struct AB { int a; int b; };\n" checkUninitVar("struct AB { int a; int b; };\n"
@ -3621,8 +3620,7 @@ private:
" do_something(a);\n" " do_something(a);\n"
" }\n" " }\n"
"}\n", "test.c"); "}\n", "test.c");
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized variable: ab\n" ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
"[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar("void f(int i) {\n" // #4569 fp checkUninitVar("void f(int i) {\n" // #4569 fp
" float *buffer;\n" " float *buffer;\n"