Fixed #6415 - FP stringCompare memcmp(ptr, ptr+offset, length).

This commit is contained in:
orbitcowboy 2015-01-07 08:30:05 +01:00
parent fd9134fe5b
commit c07b07b8fe
2 changed files with 24 additions and 3 deletions

View File

@ -40,18 +40,18 @@ void CheckString::checkAlwaysTrueOrFalseStringCompare()
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "memcmp|strncmp|strcmp|stricmp|strverscmp|bcmp|strcmpi|strcasecmp|strncasecmp|strncasecmp_l|strcasecmp_l|wcsncasecmp|wcscasecmp|wmemcmp|wcscmp|wcscasecmp_l|wcsncasecmp_l|wcsncmp|_mbscmp|_memicmp|_memicmp_l|_stricmp|_wcsicmp|_mbsicmp|_stricmp_l|_wcsicmp_l|_mbsicmp_l (")) {
if (Token::Match(tok->tokAt(2), "%str% , %str%")) {
if (Token::Match(tok->tokAt(2), "%str% , %str% ,|)")) {
const std::string &str1 = tok->strAt(2);
const std::string &str2 = tok->strAt(4);
alwaysTrueFalseStringCompareError(tok, str1, str2);
tok = tok->tokAt(5);
} else if (Token::Match(tok->tokAt(2), "%var% , %var%")) {
} else if (Token::Match(tok->tokAt(2), "%var% , %var% ,|)")) {
const std::string &str1 = tok->strAt(2);
const std::string &str2 = tok->strAt(4);
if (str1 == str2)
alwaysTrueStringVariableCompareError(tok, str1, str2);
tok = tok->tokAt(5);
} else if (Token::Match(tok->tokAt(2), "%var% . c_str ( ) , %var% . c_str ( )")) {
} else if (Token::Match(tok->tokAt(2), "%var% . c_str ( ) , %var% . c_str ( ) ,|)")) {
const std::string &str1 = tok->strAt(2);
const std::string &str2 = tok->strAt(8);
if (str1 == str2)

View File

@ -125,6 +125,27 @@ private:
"[test.cpp:12]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:13]: (warning) Unnecessary comparison of static strings.\n", errout.str());
// avoid false positives when the address is modified #6415
check("void f(void *p, int offset) {\n"
" if (!memcmp(p, p+offset, 42)){}\n"
" if (!memcmp(p+offset, p, 42)){}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// avoid false positives when the address is modified #6415
check("void f(char *c, int offset) {\n"
" if (!memcmp(c, c + offset, 42)){}\n"
" if (!memcmp(c+ offset, c , 42)){}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// avoid false positives when the address is modified #6415
check("void f(std::string s, int offset) {\n"
" if (!memcmp(s.c_str(), s.c_str() + offset, 42)){}\n"
" if (!memcmp(s.c_str() + offset, s.c_str(), 42)){}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check_preprocess_suppress(
"#define MACRO \"00FF00\"\n"
"int main()\n"