Fixed #731 (False positive, strcpy copying a buffer with a null character)

http://sourceforge.net/apps/trac/cppcheck/ticket/731
This commit is contained in:
Slava Semushin 2009-09-26 22:58:14 +07:00
parent 6d56ab9df6
commit 50a34b8a37
3 changed files with 22 additions and 0 deletions

View File

@ -510,7 +510,14 @@ size_t Token::getStrLength(const Token *tok)
while (*str)
{
if (*str == '\\')
{
++str;
// string ends at '\0'
if (*str == '0')
break;
}
++str;
++len;
}

View File

@ -94,6 +94,7 @@ private:
TEST_CASE(buffer_overrun_4);
TEST_CASE(buffer_overrun_5);
TEST_CASE(buffer_overrun_6);
TEST_CASE(buffer_overrun_7);
TEST_CASE(sprintf1);
TEST_CASE(sprintf2);
@ -580,6 +581,17 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (possible error) Buffer overrun\n", errout.str());
}
void buffer_overrun_7()
{
// ticket #731
check("void f()\n"
"{\n"
" char a[2];\n"
" strcpy(a, \"a\\0\");\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void sprintf1()
{
check("void f()\n"

View File

@ -93,6 +93,9 @@ private:
tok.str("\"test \\\\test\"");
ASSERT_EQUALS(10, Token::getStrLength(&tok));
tok.str("\"a\\0\"");
ASSERT_EQUALS(1, Token::getStrLength(&tok));
}
void strValue()