Fix #419 (missleading buffer overrun)

http://sourceforge.net/apps/trac/cppcheck/ticket/419
This commit is contained in:
Reijo Tomperi 2009-10-01 10:59:27 +03:00
parent 37fb0d8c8a
commit d4a97eb2b4
3 changed files with 9 additions and 2 deletions

View File

@ -67,6 +67,11 @@ void CheckBufferOverrun::bufferOverrun(const Token *tok)
reportError(tok, Severity::possibleError, "bufferOverrun", "Buffer overrun"); reportError(tok, Severity::possibleError, "bufferOverrun", "Buffer overrun");
} }
void CheckBufferOverrun::dangerousStdCin(const Token *tok)
{
reportError(tok, Severity::possibleError, "dangerousStdCin", "Dangerous usage of std::cin, possible buffer overrun");
}
void CheckBufferOverrun::strncatUsage(const Token *tok) void CheckBufferOverrun::strncatUsage(const Token *tok)
{ {
reportError(tok, Severity::possibleError, "strncatUsage", "Dangerous usage of strncat. Tip: the 3rd parameter means maximum number of characters to append"); reportError(tok, Severity::possibleError, "strncatUsage", "Dangerous usage of strncat. Tip: the 3rd parameter means maximum number of characters to append");
@ -514,7 +519,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const char *varname[], con
// cin.. // cin..
if (varid > 0 && Token::Match(tok, "cin >> %varid% ;", varid)) if (varid > 0 && Token::Match(tok, "cin >> %varid% ;", varid))
{ {
bufferOverrun(tok); dangerousStdCin(tok);
} }
// Function call.. // Function call..

View File

@ -75,6 +75,7 @@ private:
void arrayIndexOutOfBounds(const Token *tok); void arrayIndexOutOfBounds(const Token *tok);
void arrayIndexOutOfBounds(); void arrayIndexOutOfBounds();
void bufferOverrun(const Token *tok); void bufferOverrun(const Token *tok);
void dangerousStdCin(const Token *tok);
void strncatUsage(const Token *tok); void strncatUsage(const Token *tok);
void outOfBounds(const Token *tok, const std::string &what); void outOfBounds(const Token *tok, const std::string &what);
void sizeArgumentAsChar(const Token *tok); void sizeArgumentAsChar(const Token *tok);
@ -83,6 +84,7 @@ private:
{ {
arrayIndexOutOfBounds(0); arrayIndexOutOfBounds(0);
bufferOverrun(0); bufferOverrun(0);
dangerousStdCin(0);
strncatUsage(0); strncatUsage(0);
outOfBounds(0, "index"); outOfBounds(0, "index");
sizeArgumentAsChar(0); sizeArgumentAsChar(0);

View File

@ -827,7 +827,7 @@ private:
" char str[10];\n" " char str[10];\n"
" cin >> str;\n" " cin >> str;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (possible error) Buffer overrun\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (possible error) Dangerous usage of std::cin, possible buffer overrun\n", errout.str());
} }