|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2020 Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | +// This file is for integration tests for trac tickets. |
| 21 | +// |
| 22 | +// The intention is mostly to put tests here for false positives that |
| 23 | +// are fixed by fixing AST/ValueFlow/SymbolDatabase/etc. The check itself |
| 24 | +// worked as it should but there was problem with the input. |
| 25 | +// |
| 26 | +// These tests are typically "black box" tests and don't consider how the |
| 27 | +// checker works internally. |
| 28 | + |
| 29 | +#include "checkbool.h" |
| 30 | +#include "checknullpointer.h" |
| 31 | +#include "checkother.h" |
| 32 | +#include "testsuite.h" |
| 33 | +#include "settings.h" |
| 34 | + |
| 35 | +class TestTrac : public TestFixture { |
| 36 | +public: |
| 37 | + TestTrac() : TestFixture("TestTrac") { |
| 38 | + } |
| 39 | + |
| 40 | +private: |
| 41 | + |
| 42 | + void run() OVERRIDE { |
| 43 | + TEST_CASE(ticket_7798); |
| 44 | + TEST_CASE(ticket_9573); |
| 45 | + TEST_CASE(ticket_9700); |
| 46 | + } |
| 47 | + |
| 48 | + template<class C> |
| 49 | + void check(const char code[], const char *filename = "test.cpp") { |
| 50 | + // Clear the error buffer.. |
| 51 | + errout.str(""); |
| 52 | + |
| 53 | + Settings settings; |
| 54 | + settings.addEnabled("style"); |
| 55 | + settings.addEnabled("warning"); |
| 56 | + settings.addEnabled("portability"); |
| 57 | + settings.addEnabled("performance"); |
| 58 | + settings.standards.c = Standards::CLatest; |
| 59 | + settings.standards.cpp = Standards::CPPLatest; |
| 60 | + settings.inconclusive = true; |
| 61 | + |
| 62 | + // Tokenize.. |
| 63 | + Tokenizer tokenizer(&settings, this); |
| 64 | + std::istringstream istr(code); |
| 65 | + tokenizer.tokenize(istr, filename); |
| 66 | + |
| 67 | + // Check.. |
| 68 | + C c(&tokenizer, &settings, this); |
| 69 | + c.runChecks(&tokenizer, &settings, this); |
| 70 | + } |
| 71 | + |
| 72 | + void ticket_7798() { |
| 73 | + // checkComparisonOfFuncReturningBool (overloaded functions) |
| 74 | + check<CheckBool>("bool eval(double *) { return false; }\n" |
| 75 | + "double eval(char *) { return 1.0; }\n" |
| 76 | + "int main(int argc, char *argv[])\n" |
| 77 | + "{\n" |
| 78 | + " if ( eval(argv[1]) > eval(argv[2]) )\n" |
| 79 | + " return 1;\n" |
| 80 | + " return 0;\n" |
| 81 | + "}"); |
| 82 | + ASSERT_EQUALS("", errout.str()); |
| 83 | + } |
| 84 | + |
| 85 | + void ticket_9573() { |
| 86 | + // nullpointer (valueflow) |
| 87 | + check<CheckNullPointer>("int foo (int **array, size_t n_array) {\n" |
| 88 | + " size_t i;\n" |
| 89 | + " for (i = 0; i < n_array; ++i) {\n" |
| 90 | + " if (*array[i] == 1)\n" |
| 91 | + " return 1;\n" |
| 92 | + " }\n" |
| 93 | + " return 0;\n" |
| 94 | + "} \n" |
| 95 | + "int bar() {\n" |
| 96 | + " int **array = NULL; \n" |
| 97 | + " foo (array, 0);\n" |
| 98 | + "}\n"); |
| 99 | + ASSERT_EQUALS("", errout.str()); |
| 100 | + } |
| 101 | + |
| 102 | + void ticket_9700() { |
| 103 | + // FP: duplicateBranch |
| 104 | + check<CheckOther>("void* f(bool b) {\n" |
| 105 | + " if (b) {\n" |
| 106 | + " return new A::Y(true);\n" |
| 107 | + " } else {\n" |
| 108 | + " return new A::Z(true);\n" |
| 109 | + " }\n" |
| 110 | + "}\n"); |
| 111 | + ASSERT_EQUALS("", errout.str()); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +REGISTER_TEST(TestTrac) |
0 commit comments