Skip to content

Commit c07b07b

Browse files
committed
Fixed danmar#6415 - FP stringCompare memcmp(ptr, ptr+offset, length).
1 parent fd9134f commit c07b07b

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

lib/checkstring.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ void CheckString::checkAlwaysTrueOrFalseStringCompare()
4040

4141
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
4242
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 (")) {
43-
if (Token::Match(tok->tokAt(2), "%str% , %str%")) {
43+
if (Token::Match(tok->tokAt(2), "%str% , %str% ,|)")) {
4444
const std::string &str1 = tok->strAt(2);
4545
const std::string &str2 = tok->strAt(4);
4646
alwaysTrueFalseStringCompareError(tok, str1, str2);
4747
tok = tok->tokAt(5);
48-
} else if (Token::Match(tok->tokAt(2), "%var% , %var%")) {
48+
} else if (Token::Match(tok->tokAt(2), "%var% , %var% ,|)")) {
4949
const std::string &str1 = tok->strAt(2);
5050
const std::string &str2 = tok->strAt(4);
5151
if (str1 == str2)
5252
alwaysTrueStringVariableCompareError(tok, str1, str2);
5353
tok = tok->tokAt(5);
54-
} else if (Token::Match(tok->tokAt(2), "%var% . c_str ( ) , %var% . c_str ( )")) {
54+
} else if (Token::Match(tok->tokAt(2), "%var% . c_str ( ) , %var% . c_str ( ) ,|)")) {
5555
const std::string &str1 = tok->strAt(2);
5656
const std::string &str2 = tok->strAt(8);
5757
if (str1 == str2)

test/teststring.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ class TestString : public TestFixture {
125125
"[test.cpp:12]: (warning) Unnecessary comparison of static strings.\n"
126126
"[test.cpp:13]: (warning) Unnecessary comparison of static strings.\n", errout.str());
127127

128+
// avoid false positives when the address is modified #6415
129+
check("void f(void *p, int offset) {\n"
130+
" if (!memcmp(p, p+offset, 42)){}\n"
131+
" if (!memcmp(p+offset, p, 42)){}\n"
132+
"}\n");
133+
ASSERT_EQUALS("", errout.str());
134+
135+
// avoid false positives when the address is modified #6415
136+
check("void f(char *c, int offset) {\n"
137+
" if (!memcmp(c, c + offset, 42)){}\n"
138+
" if (!memcmp(c+ offset, c , 42)){}\n"
139+
"}\n");
140+
ASSERT_EQUALS("", errout.str());
141+
142+
// avoid false positives when the address is modified #6415
143+
check("void f(std::string s, int offset) {\n"
144+
" if (!memcmp(s.c_str(), s.c_str() + offset, 42)){}\n"
145+
" if (!memcmp(s.c_str() + offset, s.c_str(), 42)){}\n"
146+
"}\n");
147+
ASSERT_EQUALS("", errout.str());
148+
128149
check_preprocess_suppress(
129150
"#define MACRO \"00FF00\"\n"
130151
"int main()\n"

0 commit comments

Comments
 (0)