diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index 690356535..c9dd8760b 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -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) diff --git a/test/teststring.cpp b/test/teststring.cpp index b7f3db7bf..8c044866f 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -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"