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-2026 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 "errortypes.h" #include "helpers.h" #include "settings.h" #include "fixture.h" #include #include class TestString : public TestFixture { public: TestString() : TestFixture("TestString") {} private: const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).build(); void run() override { mNewTemplate = true; 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); } struct CheckOptions { bool cpp = true; }; #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { SimpleTokenizer2 tokenizer(settings, *this, code, options.cpp ? "test.cpp" : "test.c"); // Tokenize.. ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line); CheckString check; runChecks(check, tokenizer, *this); } void stringLiteralWrite() { check("void f() {\n" " char *abc = \"abc\";\n" " abc[0] = 'a';\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:3] -> [test.cpp:2:15]: (error) Modifying string literal \"abc\" directly or indirectly is undefined behaviour. [stringLiteralWrite]\n", errout_str()); check("void f() {\n" " char *abc = \"abc\";\n" " *abc = 'a';\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:4] -> [test.cpp:2:15]: (error) Modifying string literal \"abc\" directly or indirectly is undefined behaviour. [stringLiteralWrite]\n", errout_str()); check("void f() {\n" " char *abc = \"A very long string literal\";\n" " abc[0] = 'a';\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:3] -> [test.cpp:2:15]: (error) Modifying string literal \"A very long stri..\" directly or indirectly is undefined behaviour. [stringLiteralWrite]\n", errout_str()); check("void f() {\n" " QString abc = \"abc\";\n" " abc[0] = 'a';\n" "}\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" "}\n"); ASSERT_EQUALS( "[test.cpp:2:3] -> [test.cpp:5:13]: (error) Modifying string literal \"Y\" directly or indirectly is undefined behaviour. [stringLiteralWrite]\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" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " wchar_t *abc = L\"abc\";\n" " abc[0] = u'a';\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:3] -> [test.cpp:2:18]: (error) Modifying string literal L\"abc\" directly or indirectly is undefined behaviour. [stringLiteralWrite]\n", errout_str()); check("void f() {\n" " char16_t *abc = u\"abc\";\n" " abc[0] = 'a';\n" "}\n"); ASSERT_EQUALS("[test.cpp:3:3] -> [test.cpp:2:19]: (error) Modifying string literal u\"abc\" directly or indirectly is undefined behaviour. [stringLiteralWrite]\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" //

Back | FazBrowse Home | New Git URL