From 2e45df3b72d1fda2dd3058e44608d9df7e7bbb50 Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Wed, 17 Dec 2014 17:43:31 +0100 Subject: [PATCH] Fixed #6341: false negative uninitvar pattern "return foo ( %var%" --- cfg/posix.cfg | 9 +++++++++ lib/checkuninitvar.cpp | 9 ++++++--- test/testuninitvar.cpp | 10 ++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/cfg/posix.cfg b/cfg/posix.cfg index 0a7a888ea..db63a3e29 100644 --- a/cfg/posix.cfg +++ b/cfg/posix.cfg @@ -1,5 +1,14 @@ + + + + false + + + + + false diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 7b8e29ceb..eb90e1bcf 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1449,9 +1449,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, uninitdataError(tok, tok->str()); else uninitvarError(tok, tok->str()); - } - - else + } else // assume that variable is assigned return true; } @@ -1666,6 +1664,11 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all if (!alloc && vartok->previous()->str() == "return") 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__ if (Token::Match(vartok->tokAt(-3), "typeof|__alignof__ ( * %var%")) return false; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 49f4e2d63..b8616ad3e 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -73,6 +73,7 @@ private: TEST_CASE(syntax_error); // Ticket #5073 // Test that the functions from std.cfg are configured correctly + TEST_CASE(stdcfg_btowc); TEST_CASE(stdcfg_clearerr); TEST_CASE(stdcfg_fclose); TEST_CASE(stdcfg_fopen); @@ -3610,6 +3611,15 @@ private: 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. void stdcfg_clearerr() { checkUninitVar("void f() {\n"