FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GitHub Viewer

/* * Cppcheck - A tool for static C/C++ code analysis * Copyright (C) 2007-2015 Daniel Marjamäki and 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 "tokenize.h" #include "checkstring.h" #include "testsuite.h" #include "preprocessor.h" #include "testutils.h" #include extern std::ostringstream errout; class TestString : public TestFixture { public: TestString() : TestFixture("TestString") { } private: void run() { 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); // ast TEST_CASE(sprintf1); // Dangerous usage of sprintf TEST_CASE(sprintf2); TEST_CASE(sprintf3); TEST_CASE(sprintf4); // struct member TEST_CASE(incorrectStringCompare); } void check(const char code[], const char filename[] = "test.cpp") { // Clear the error buffer.. errout.str(""); Settings settings; settings.addEnabled("warning"); settings.addEnabled("style"); // 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); tokenizer.simplifyTokenList2(); checkString.runSimplifiedChecks(&tokenizer, &settings, this); } void check_preprocess_suppress(const char precode[]) { // Clear the error buffer.. errout.str(""); Settings settings; settings.addEnabled("warning"); // Preprocess file.. Preprocessor preprocessor(&settings, this); std::list configurations; std::string filedata; std::istringstream fin(precode); preprocessor.preprocess(fin, filedata, configurations, "test.cpp", settings._includePaths); const std::string code = preprocessor.getcode(filedata, "", "test.cpp"); // Tokenize.. Tokenizer tokenizer(&settings, this); std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); // Check.. CheckString checkString(&tokenizer, &settings, this); checkString.checkAlwaysTrueOrFalseStringCompare(); } 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" "}\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" "}\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" "}\n"); ASSERT_EQUALS("", errout.str()); check_preprocess_suppress( "#define MACRO \"00FF00\"\n" "int main()\n" "{\n" " if (strcmp(MACRO,\"00FF00\") == 0)" " {" " std::cout

Back | FazBrowse Home | New Git URL