Uninitalized variables: Fixed false positive

This commit is contained in:
Daniel Marjamäki 2018-08-15 18:04:36 +02:00
parent 3a90f1fc41
commit fc97a5b11c
2 changed files with 14 additions and 1 deletions

View File

@ -1076,8 +1076,12 @@ int CheckUninitVar::isFunctionParUsage(const Token *vartok, bool pointer, Alloc
return 1;
while (argStart->previous() && argStart->previous()->isName())
argStart = argStart->previous();
if (Token::Match(argStart, "const %type% & %name% [,)]"))
if (Token::Match(argStart, "const %type% & %name% [,)]")) {
// If it's a record it's ok to pass a partially uninitialized struct.
if (vartok->variable() && vartok->variable()->valueType() && vartok->variable()->valueType()->type == ValueType::Type::RECORD)
return -1;
return 1;
}
if ((pointer || address) && alloc == NO_ALLOC && Token::Match(argStart, "const struct| %type% * %name% [,)]"))
return 1;
if ((pointer || address) && Token::Match(argStart, "const %type% %name% [") && Token::Match(argStart->linkAt(3), "] [,)]"))

View File

@ -3048,6 +3048,15 @@ private:
"}\n", "test.c");
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar("struct AB { int a; int b; };\n"
"void do_something(const struct AB &ab) { a = ab.a; }\n"
"void f(void) {\n"
" struct AB ab;\n"
" ab.a = 0;\n"
" do_something(ab);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("struct AB { int a; int b; };\n"
"void f(void) {\n"
" struct AB ab;\n"