ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Modifying string literal \"abc\" directly or indirectly is undefined behaviour.\n", errout_str());
check("void f() {\n"
" char *abc = \"abc\";\n"
" *abc = 'a';\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Modifying string literal \"abc\" directly or indirectly is undefined behaviour.\n", errout_str());
check("void f() {\n"
" char *abc = \"A very long string literal\";\n"
" abc[0] = 'a';\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Modifying string literal \"A very long stri..\" directly or indirectly is undefined behaviour.\n", errout_str());
check("void f() {\n"
" QString abc = \"abc\";\n"
" abc[0] = 'a';\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void foo_FP1(char *p) {\n"
" p[1] = 'B';\n"
"}\n"
"void foo_FP2(void) {\n"
" char* s = \"Y\";\n"
" foo_FP1(s);\n"
"}");
ASSERT_EQUALS(
"[test.cpp:2] -> [test.cpp:5]: (error) Modifying string literal \"Y\" directly or indirectly is undefined behaviour.\n",
errout_str());
check("void foo_FP1(char *p) {\n"
" p[1] = 'B';\n"
"}\n"
"void foo_FP2(void) {\n"
" char s[10] = \"Y\";\n"
" foo_FP1(s);\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" wchar_t *abc = L\"abc\";\n"
" abc[0] = u'a';\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Modifying string literal L\"abc\" directly or indirectly is undefined behaviour.\n", errout_str());
check("void f() {\n"
" char16_t *abc = u\"abc\";\n"
" abc[0] = 'a';\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Modifying string literal u\"abc\" directly or indirectly is undefined behaviour.\n", errout_str());
check("void foo() {\n"// #8332
" int i;\n"
" char *p = \"string literal\";\n"
" for( i = 0; i < strlen(p); i++) {\n"
" p[i] = \'X\';\n"// <<
" }\n"
" printf(\"%s\\n\", p);\n"
"}");
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (error) Modifying string literal \"string literal\" directly or indirectly is undefined behaviour.\n", errout_str());
}
voidalwaysTrueFalseStringCompare() {
check("void f() {\n"
" if (strcmp(\"A\",\"A\")){}\n"
" if (strncmp(\"A\",\"A\",1)){}\n"
" if (strcasecmp(\"A\",\"A\")){}\n"
" if (strncasecmp(\"A\",\"A\",1)){}\n"
" if (memcmp(\"A\",\"A\",1)){}\n"
" if (strverscmp(\"A\",\"A\")){}\n"
" if (bcmp(\"A\",\"A\",1)){}\n"
" if (wcsncasecmp(L\"A\",L\"A\",1)){}\n"
" if (wcsncmp(L\"A\",L\"A\",1)){}\n"
" if (wmemcmp(L\"A\",L\"A\",1)){}\n"
" if (wcscmp(L\"A\",L\"A\")){}\n"
" if (wcscasecmp(L\"A\",L\"A\")){}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:4]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:5]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:6]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:7]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:8]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:9]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:10]: (warning) Unnecessary comparison of static strings.\n"
"[test.cpp:11]: (warning) Unnecessary comparison of static strings.\n"
"[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"
" if (!memcmp(offset + p, p, 42)){}\n"
" if (!memcmp(p, offset + p, 42)){}\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"
" if (!memcmp(offset + c, c, 42)){}\n"
" if (!memcmp(c, offset + c, 42)){}\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"
" if (!memcmp(offset + s.c_str(), s.c_str(), 42)){}\n"
" if (!memcmp(s.c_str(), offset + s.c_str(), 42)){}\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int main()\n"
"{\n"
" if (strcmp(\"00FF00\", \"00FF00\") == 0)"
" {"
" std::cout << \"Equal\";"
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout_str());
check("void f() {\n"
" if (strcmp($\"00FF00\", \"00FF00\") == 0) {}"
"}");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" if ($strcmp(\"00FF00\", \"00FF00\") == 0) {}"
"}");
ASSERT_EQUALS("", errout_str());
check("int main()\n"
"{\n"
" if (stricmp(\"hotdog\",\"HOTdog\") == 0)"
" {"
" std::cout << \"Equal\";"
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout_str());
check("int main()\n"
"{\n"
" if (QString::compare(\"Hamburger\", \"Hotdog\") == 0)"
" {"
" std::cout << \"Equal\";"
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout_str());
check("int main()\n"
"{\n"
" if (QString::compare(argv[2], \"hotdog\") == 0)"
" {"
" std::cout << \"Equal\";"
" }"
"}");
ASSERT_EQUALS("", errout_str());
check("int main()\n"
"{\n"
" if (strncmp(\"hotdog\",\"hotdog\", 6) == 0)"
" {"
" std::cout << \"Equal\";"
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Unnecessary comparison of static strings.\n", errout_str());
check("int foo(const char *buf)\n"
"{\n"
" if (strcmp(buf, buf) == 0)"
" {"
" std::cout << \"Equal\";"
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of identical string variables.\n", errout_str());
check("int foo(const std::string& buf)\n"
"{\n"
" if (stricmp(buf.c_str(), buf.c_str()) == 0)"
" {"
" std::cout << \"Equal\";"
" }"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of identical string variables.\n", errout_str());
check("int main() {\n"
" if (\"str\" == \"str\") {\n"
" std::cout << \"Equal\";\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Unnecessary comparison of static strings.\n", errout_str());
check("int main() {\n"
" if (\"str\" != \"str\") {\n"
" std::cout << \"Equal\";\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Unnecessary comparison of static strings.\n", errout_str());
check("int main() {\n"
" if (a+\"str\" != \"str\"+b) {\n"
" std::cout << \"Equal\";\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
}
voidsuspiciousStringCompare() {
check("bool foo(char* c) {\n"
" return c == \"x\";\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead?\n", errout_str());
check("bool foo(char** c) {\n"
" return c[3] == \"x\";\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) String literal compared with variable 'c[3]'. Did you intend to use strcmp() instead?\n", errout_str());
check("bool foo(wchar_t* c) {\n"
" return c == L\"x\";\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use wcscmp() instead?\n", errout_str());
check("bool foo(const char* c) {\n"
" return \"x\" == c;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead?\n", errout_str());
check("bool foo(char* c) {\n"
" return foo+\"x\" == c;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("bool foo(char* c) {\n"
" return \"x\" == c+foo;\n"
"}", "test.cpp");
ASSERT_EQUALS("", errout_str());
check("bool foo(char* c) {\n"
" return \"x\" == c+foo;\n"
"}", "test.c");
ASSERT_EQUALS("[test.c:2]: (warning) String literal compared with variable 'c+foo'. Did you intend to use strcmp() instead?\n", errout_str());
check("bool foo(Foo c) {\n"
" return \"x\" == c.foo;\n"
"}", "test.cpp");
ASSERT_EQUALS("", errout_str());
check("bool foo(Foo c) {\n"
" return \"x\" == c.foo;\n"
"}", "test.c");
ASSERT_EQUALS("[test.c:2]: (warning) String literal compared with variable 'c.foo'. Did you intend to use strcmp() instead?\n", errout_str());
check("bool foo(const std::string& c) {\n"
" return \"x\" == c;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("bool foo(const Foo* c) {\n"
" return \"x\" == c->bar();\n"// #4314
"}");
ASSERT_EQUALS("", errout_str());
// Ticket #4257
check("bool foo() {\n"
"MyString *str=Getter();\n"
"return *str==\"bug\"; }\n", "test.c");
ASSERT_EQUALS("[test.c:3]: (warning) String literal compared with variable '*str'. Did you intend to use strcmp() instead?\n", errout_str());
// Ticket #4257
check("bool foo() {\n"
"MyString *str=Getter();\n"
"return *str==\"bug\"; }");
ASSERT_EQUALS("", errout_str());
// Ticket #4257
check("bool foo() {\n"
"MyString **str=OtherGetter();\n"
"return *str==\"bug\"; }", "test.c");
ASSERT_EQUALS("[test.c:3]: (warning) String literal compared with variable '*str'. Did you intend to use strcmp() instead?\n", errout_str());
// Ticket #4257
check("bool foo() {\n"
"MyString str=OtherGetter2();\n"
"return &str==\"bug\"; }", "test.c");
ASSERT_EQUALS("[test.c:3]: (warning) String literal compared with variable '&str'. Did you intend to use strcmp() instead?\n", errout_str());
// Ticket #5734
check("int foo(char c) {\n"
"return c == '4';}", "test.cpp");
ASSERT_EQUALS("", errout_str());
check("int foo(char c) {\n"
"return c == '4';}", "test.c");
ASSERT_EQUALS("", errout_str());
check("int foo(char c) {\n"
"return c == \"42\"[0];}", "test.cpp");
ASSERT_EQUALS("", errout_str());
check("int foo(char c) {\n"
"return c == \"42\"[0];}", "test.c");
ASSERT_EQUALS("", errout_str());
// 5639 String literal compared with char buffer in a struct
check("struct Example {\n"
" char buffer[200];\n"
"};\n"
"void foo() {\n"
" struct Example example;\n"
" if (example.buffer == \"test\") ;\n"
"}\n", "test.cpp");
ASSERT_EQUALS("[test.cpp:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout_str());
check("struct Example {\n"
" char buffer[200];\n"
"};\n"
"void foo() {\n"
" struct Example example;\n"
" if (example.buffer == \"test\") ;\n"
"}\n", "test.c");
ASSERT_EQUALS("[test.c:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout_str());
" if (strcmp(str, \"abc\") == 0 || strcmp(str, \"def\")) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) The expression 'strcmp(str,\"def\") != 0' is suspicious. It overlaps 'strcmp(str,\"abc\") == 0'.\n", errout_str());
check("void f(const wchar_t *str) {\n"
" if (wcscmp(str, L\"abc\") == 0 || wcscmp(str, L\"def\")) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) The expression 'wcscmp(str,L\"def\") != 0' is suspicious. It overlaps 'wcscmp(str,L\"abc\") == 0'.\n", errout_str());
check("struct X {\n"
" char *str;\n"
"};\n"
"\n"
"void f(const struct X *x) {\n"
" if (strcmp(x->str, \"abc\") == 0 || strcmp(x->str, \"def\")) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:6]: (warning) The expression 'strcmp(x->str,\"def\") != 0' is suspicious. It overlaps 'strcmp(x->str,\"abc\") == 0'.\n", errout_str());