GitHub Viewer
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2021 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "checkstring.h"
#include "settings.h"
#include "testsuite.h"
#include "tokenize.h"
class TestString : public TestFixture {
public:
TestString() : TestFixture("TestString") {}
private:
Settings settings;
void run() OVERRIDE {
settings.severity.enable(Severity::warning);
settings.severity.enable(Severity::style);
TEST_CASE(stringLiteralWrite);
TEST_CASE(alwaysTrueFalseStringCompare);
TEST_CASE(suspiciousStringCompare);
TEST_CASE(suspiciousStringCompare_char);
TEST_CASE(strPlusChar1); // "/usr" + '/'
TEST_CASE(strPlusChar2); // "/usr" + ch
TEST_CASE(strPlusChar3); // ok: path + "/sub" + '/'
TEST_CASE(strPlusChar4); // L"/usr" + L'/'
TEST_CASE(snprintf1); // Dangerous usage of snprintf
TEST_CASE(sprintf1); // Dangerous usage of sprintf
TEST_CASE(sprintf2);
TEST_CASE(sprintf3);
TEST_CASE(sprintf4); // struct member
TEST_CASE(sprintf5); // another struct member
TEST_CASE(sprintf6); // (char*)
TEST_CASE(sprintf7); // (char*)(void*)
TEST_CASE(wsprintf1); // Dangerous usage of wsprintf
TEST_CASE(incorrectStringCompare);
TEST_CASE(deadStrcmp);
}
void check(const char code[], const char filename[] = "test.cpp") {
// Clear the error buffer..
errout.str("");
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
// Check char variable usage..
CheckString checkString(&tokenizer, &settings, this);
checkString.runChecks(&tokenizer, &settings, this);
}
void stringLiteralWrite() {
check("void f() {\n"
" char *abc = \"abc\";\n"
" abc[0] = '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 = \"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());
}
void alwaysTrueFalseStringCompare() {
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