Fixed #1818 (False positive: Dangerous usage of strncpy (copying a constant string))

This commit is contained in:
Daniel Marjamäki 2010-07-05 12:45:39 +02:00
parent 643d400716
commit c811acaa50
2 changed files with 34 additions and 2 deletions

View File

@ -3164,8 +3164,24 @@ private:
// strncpy doesn't 0-terminate first parameter
if (Token::Match(&tok, "strncpy ( %var% ,"))
{
init_strncpy(checks, tok.tokAt(2));
return tok.next()->link();
if (Token::Match(tok.tokAt(4), "%str% ,"))
{
if (Token::Match(tok.tokAt(6), "%num% )"))
{
const unsigned int len = Token::getStrLength(tok.tokAt(4));
const unsigned int sz = MathLib::toLongNumber(tok.strAt(6));
if (len>=sz)
{
init_strncpy(checks, tok.tokAt(2));
return tok.next()->link();
}
}
}
else
{
init_strncpy(checks, tok.tokAt(2));
return tok.next()->link();
}
}
if (Token::simpleMatch(&tok, "asm ( )"))

View File

@ -2065,6 +2065,22 @@ private:
" strncat(a, s, 20);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of 'a' (strncpy doesn't always 0-terminate it)\n", errout.str());
checkUninitVar("void f()\n"
"{\n"
" char a[100];\n"
" strncpy(a, \"hello\", 3);\n"
" strncat(a, \"world\", 20);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of 'a' (strncpy doesn't always 0-terminate it)\n", errout.str());
checkUninitVar("void f()\n"
"{\n"
" char a[100];\n"
" strncpy(a, \"hello\", sizeof(a));\n"
" strncat(a, \"world\", 20);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}