Fixed #6341: false negative uninitvar pattern "return foo ( %var%"

This commit is contained in:
orbitcowboy 2014-12-17 17:43:31 +01:00 committed by PKEuS
parent 4cbbca3db0
commit 2e45df3b72
3 changed files with 25 additions and 3 deletions

View File

@ -1,5 +1,14 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<def format="1"> <def format="1">
<!-- wint_t btowc (int c); -->
<!-- A function to convert a single byte character to a wide character. -->
<function name="btowc">
<noreturn>false</noreturn>
<arg nr="1">
<not-uninit/>
</arg>
<leak-ignore/>
</function>
<function name="usleep"> <function name="usleep">
<noreturn>false</noreturn> <noreturn>false</noreturn>
<arg nr="1"> <arg nr="1">

View File

@ -1449,9 +1449,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
uninitdataError(tok, tok->str()); uninitdataError(tok, tok->str());
else else
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());
} } else
else
// assume that variable is assigned // assume that variable is assigned
return true; return true;
} }
@ -1666,6 +1664,11 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
if (!alloc && vartok->previous()->str() == "return") if (!alloc && vartok->previous()->str() == "return")
return true; return true;
// code like: return foo( variable
if (vartok->tokAt(-3) && vartok->tokAt(-3)->str() == "return"
&& vartok->tokAt(-1) && vartok->tokAt(-1)->str() == "(")
return true;
// Passing variable to typeof/__alignof__ // Passing variable to typeof/__alignof__
if (Token::Match(vartok->tokAt(-3), "typeof|__alignof__ ( * %var%")) if (Token::Match(vartok->tokAt(-3), "typeof|__alignof__ ( * %var%"))
return false; return false;

View File

@ -73,6 +73,7 @@ private:
TEST_CASE(syntax_error); // Ticket #5073 TEST_CASE(syntax_error); // Ticket #5073
// Test that the functions from std.cfg are configured correctly // Test that the functions from std.cfg are configured correctly
TEST_CASE(stdcfg_btowc);
TEST_CASE(stdcfg_clearerr); TEST_CASE(stdcfg_clearerr);
TEST_CASE(stdcfg_fclose); TEST_CASE(stdcfg_fclose);
TEST_CASE(stdcfg_fopen); TEST_CASE(stdcfg_fopen);
@ -3610,6 +3611,15 @@ private:
ASSERT_EQUALS("[test.cpp:6]: (debug) assertion failed '} while ('\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (debug) assertion failed '} while ('\n", errout.str());
} }
// Test that the btowc function, defined in std.cfg is configured correctly.
void stdcfg_btowc() {
checkUninitVar2("wchar_t f() { int i; return btowc(i); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Uninitialized variable: i\n", errout.str());
checkUninitVar2("wchar_t f(int i) { return btowc(i); }");
ASSERT_EQUALS("", errout.str());
}
// Test that the clearerr function, defined in std.cfg is configured correctly. // Test that the clearerr function, defined in std.cfg is configured correctly.
void stdcfg_clearerr() { void stdcfg_clearerr() {
checkUninitVar("void f() {\n" checkUninitVar("void f() {\n"