2013-05-07 21:35:16 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 Cppcheck team.
|
2013-05-07 21:35:16 +02:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2013-05-07 21:35:16 +02:00
|
|
|
#include "checkassert.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "errortypes.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "settings.h"
|
2013-05-07 21:35:16 +02:00
|
|
|
#include "testsuite.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "tokenize.h"
|
2013-05-07 21:35:16 +02:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <iosfwd>
|
2022-08-19 18:23:15 +02:00
|
|
|
#include <sstream>
|
2022-01-27 19:03:20 +01:00
|
|
|
|
2013-05-07 21:35:16 +02:00
|
|
|
|
|
|
|
class TestAssert : public TestFixture {
|
|
|
|
public:
|
2014-08-06 10:15:48 +02:00
|
|
|
TestAssert() : TestFixture("TestAssert") {}
|
2013-05-07 21:35:16 +02:00
|
|
|
|
|
|
|
private:
|
2015-10-07 18:33:57 +02:00
|
|
|
Settings settings;
|
|
|
|
|
2021-11-29 07:34:39 +01:00
|
|
|
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
|
|
|
|
void check_(const char* file, int line, const char code[], const char *filename = "test.cpp") {
|
2013-05-07 21:35:16 +02:00
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
2021-11-29 07:34:39 +01:00
|
|
|
ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);
|
2013-05-07 21:35:16 +02:00
|
|
|
|
|
|
|
// Check..
|
2019-03-09 16:50:50 +01:00
|
|
|
CheckAssert checkAssert;
|
|
|
|
checkAssert.runChecks(&tokenizer, &settings, this);
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
void run() override {
|
2021-02-24 22:00:06 +01:00
|
|
|
settings.severity.enable(Severity::warning);
|
2015-10-07 18:33:57 +02:00
|
|
|
|
2013-05-07 21:35:16 +02:00
|
|
|
TEST_CASE(assignmentInAssert);
|
|
|
|
TEST_CASE(functionCallInAssert);
|
2014-08-06 10:15:48 +02:00
|
|
|
TEST_CASE(memberFunctionCallInAssert);
|
2013-05-07 21:35:16 +02:00
|
|
|
TEST_CASE(safeFunctionCallInAssert);
|
2020-09-24 20:48:26 +02:00
|
|
|
TEST_CASE(crash);
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void safeFunctionCallInAssert() {
|
2013-05-07 21:35:16 +02:00
|
|
|
check(
|
|
|
|
"int a;\n"
|
|
|
|
"bool b = false;\n"
|
|
|
|
"int foo() {\n"
|
2013-05-08 02:05:08 +02:00
|
|
|
" if (b) { a = 1+2 };\n"
|
2013-05-07 21:35:16 +02:00
|
|
|
" return a;\n"
|
|
|
|
"}\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"assert(foo() == 3);");
|
2013-05-07 21:35:16 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-06-05 22:10:43 +02:00
|
|
|
|
|
|
|
check(
|
|
|
|
"int foo(int a) {\n"
|
|
|
|
" int b=a+1;\n"
|
|
|
|
" return b;\n"
|
|
|
|
"}\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"assert(foo(1) == 2);");
|
2013-06-05 22:10:43 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void functionCallInAssert() {
|
2013-05-07 21:35:16 +02:00
|
|
|
check(
|
|
|
|
"int a;\n"
|
|
|
|
"int foo() {\n"
|
|
|
|
" a = 1+2;\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"assert(foo() == 3);");
|
2013-05-07 21:35:16 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (warning) Assert statement calls a function which may have desired side effects: 'foo'.\n", errout.str());
|
2013-08-22 06:38:54 +02:00
|
|
|
|
|
|
|
// Ticket #4937 "false positive: Assert calls a function which may have desired side effects"
|
|
|
|
check("struct SquarePack {\n"
|
|
|
|
" static bool isRank1Or8( Square sq ) {\n"
|
|
|
|
" sq &= 0x38;\n"
|
|
|
|
" return sq == 0 || sq == 0x38;\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" assert( !SquarePack::isRank1Or8(push2) );\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-08-22 06:38:54 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct SquarePack {\n"
|
|
|
|
" static bool isRank1Or8( Square &sq ) {\n"
|
|
|
|
" sq &= 0x38;\n"
|
|
|
|
" return sq == 0 || sq == 0x38;\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" assert( !SquarePack::isRank1Or8(push2) );\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-08-22 06:38:54 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'.\n", errout.str());
|
|
|
|
|
|
|
|
check("struct SquarePack {\n"
|
|
|
|
" static bool isRank1Or8( Square *sq ) {\n"
|
|
|
|
" *sq &= 0x38;\n"
|
|
|
|
" return *sq == 0 || *sq == 0x38;\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" assert( !SquarePack::isRank1Or8(push2) );\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-08-22 06:38:54 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (warning) Assert statement calls a function which may have desired side effects: 'isRank1Or8'.\n", errout.str());
|
|
|
|
|
|
|
|
check("struct SquarePack {\n"
|
|
|
|
" static bool isRank1Or8( Square *sq ) {\n"
|
|
|
|
" sq &= 0x38;\n"
|
|
|
|
" return sq == 0 || sq == 0x38;\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" assert( !SquarePack::isRank1Or8(push2) );\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2014-08-06 10:15:48 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void memberFunctionCallInAssert() {
|
2014-08-06 10:15:48 +02:00
|
|
|
check("struct SquarePack {\n"
|
|
|
|
" void Foo();\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo(SquarePack s) {\n"
|
2021-08-29 20:45:31 +02:00
|
|
|
" assert( s.Foo() );\n"
|
2014-08-06 10:15:48 +02:00
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (warning) Assert statement calls a function which may have desired side effects: 'Foo'.\n", errout.str());
|
|
|
|
|
|
|
|
check("struct SquarePack {\n"
|
|
|
|
" void Foo() const;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo(SquarePack* s) {\n"
|
2021-08-29 20:45:31 +02:00
|
|
|
" assert( s->Foo() );\n"
|
2014-08-06 10:15:48 +02:00
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct SquarePack {\n"
|
|
|
|
" static void Foo();\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo(SquarePack* s) {\n"
|
2021-08-29 20:45:31 +02:00
|
|
|
" assert( s->Foo() );\n"
|
2014-08-06 10:15:48 +02:00
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct SquarePack {\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo(SquarePack* s) {\n"
|
2021-08-29 20:45:31 +02:00
|
|
|
" assert( s->Foo() );\n"
|
2014-08-06 10:15:48 +02:00
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void assignmentInAssert() {
|
2013-05-07 21:35:16 +02:00
|
|
|
check("void f() {\n"
|
|
|
|
" int a; a = 0;\n"
|
|
|
|
" assert(a = 2);\n"
|
|
|
|
" return a;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-05-07 21:35:16 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
|
|
|
|
|
|
|
|
check("void f(int a) {\n"
|
|
|
|
" assert(a == 2);\n"
|
|
|
|
" return a;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-05-07 21:35:16 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void f(int a, int b) {\n"
|
2020-04-18 09:26:24 +02:00
|
|
|
" assert(a == 2 && (b = 1));\n"
|
2013-05-07 21:35:16 +02:00
|
|
|
" return a;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-05-07 21:35:16 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (warning) Assert statement modifies 'b'.\n", errout.str());
|
|
|
|
|
|
|
|
check("void f() {\n"
|
|
|
|
" int a; a = 0;\n"
|
|
|
|
" assert(a += 2);\n"
|
|
|
|
" return a;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-05-07 21:35:16 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
|
|
|
|
|
|
|
|
check("void f() {\n"
|
|
|
|
" int a; a = 0;\n"
|
|
|
|
" assert(a *= 2);\n"
|
|
|
|
" return a;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-05-07 21:35:16 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
|
|
|
|
|
|
|
|
check("void f() {\n"
|
|
|
|
" int a; a = 0;\n"
|
|
|
|
" assert(a -= 2);\n"
|
|
|
|
" return a;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-05-07 21:35:16 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
|
|
|
|
|
|
|
|
check("void f() {\n"
|
|
|
|
" int a = 0;\n"
|
|
|
|
" assert(a--);\n"
|
|
|
|
" return a;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2013-05-07 21:35:16 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
|
2016-05-26 18:29:29 +02:00
|
|
|
|
2020-04-18 09:38:23 +02:00
|
|
|
check("void f() {\n"
|
2020-04-18 09:26:24 +02:00
|
|
|
" int a = 0;\n"
|
|
|
|
" assert(--a);\n"
|
|
|
|
" return a;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2020-04-18 09:26:24 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Assert statement modifies 'a'.\n", errout.str());
|
|
|
|
|
2016-05-26 18:29:29 +02:00
|
|
|
check("void f() {\n"
|
|
|
|
" assert(std::all_of(first, last, []() {\n"
|
|
|
|
" auto tmp = x.someValue();\n"
|
|
|
|
" auto const expected = someOtherValue;\n"
|
|
|
|
" return tmp == expected;\n"
|
|
|
|
" }));\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2016-05-26 18:29:29 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
2020-09-24 20:48:26 +02:00
|
|
|
|
|
|
|
void crash() {
|
|
|
|
check("void foo() {\n"
|
|
|
|
" assert(sizeof(struct { int a[x++]; })==sizeof(int));\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-11-23 23:07:15 +01:00
|
|
|
|
|
|
|
check("void foo() {\n" // #9790
|
|
|
|
" assert(kad_bucket_hash(&(kad_guid) { .bytes = { 0 } }, & (kad_guid){.bytes = { 0 }}) == -1);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2020-09-24 20:48:26 +02:00
|
|
|
}
|
2013-05-07 21:35:16 +02:00
|
|
|
};
|
|
|
|
|
2013-06-05 22:10:43 +02:00
|
|
|
REGISTER_TEST(TestAssert)
|