Improved memset with 0 bytes check. TODO done (unit test).

Patch from: PKEuS <philipp.kloke@web.de>
This commit is contained in:
Reijo Tomperi 2011-10-22 23:34:10 +03:00
parent 91c6608175
commit 3568b5a841
2 changed files with 14 additions and 8 deletions

View File

@ -1337,10 +1337,12 @@ void CheckOther::udivError(const Token *tok)
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckOther::checkMemsetZeroBytes() void CheckOther::checkMemsetZeroBytes()
{ {
const Token *tok = _tokenizer->tokens(); for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
while (tok && ((tok = Token::findmatch(tok, "memset ( %var% , %num% , 0 )")) != NULL)) { if (Token::simpleMatch(tok, "memset (")) {
const Token* lastParamTok = tok->tokAt(1)->link()->tokAt(-1);
if (lastParamTok->str() == "0")
memsetZeroBytesError(tok, tok->strAt(2)); memsetZeroBytesError(tok, tok->strAt(2));
tok = tok->tokAt(8); }
} }
} }

View File

@ -2701,7 +2701,7 @@ private:
void memsetZeroBytes() { void memsetZeroBytes() {
check("void f() {\n" check("void f() {\n"
" memset(p, 10, 0);\n" " memset(p, 10, 0x0);\n"
"}\n" "}\n"
); );
ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0" ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0"
@ -2711,10 +2711,14 @@ private:
" memset(p, sizeof(p), 0);\n" " memset(p, sizeof(p), 0);\n"
"}\n" "}\n"
); );
TODO_ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0" ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0"
" bytes of \"p\". Second and third arguments might be inverted.\n", " bytes of \'p\'\n", errout.str());
"", errout.str()); check("void f() {\n"
" memset(p, sizeof(p), i+0);\n"
"}\n"
);
ASSERT_EQUALS("", errout.str());
} }
void sizeofForArrayParameter() { void sizeofForArrayParameter() {