2011-07-06 08:55:17 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2011-07-06 08:55:17 +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
|
|
|
|
2011-07-06 08:55:17 +02:00
|
|
|
#include "check64bit.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "errortypes.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "settings.h"
|
2023-01-27 08:18:32 +01:00
|
|
|
#include "fixture.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "tokenize.h"
|
2011-07-06 08:55:17 +02:00
|
|
|
|
2022-09-16 07:15:49 +02:00
|
|
|
#include <sstream> // IWYU pragma: keep
|
2011-07-06 08:55:17 +02:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class Test64BitPortability : public TestFixture {
|
2011-07-06 08:55:17 +02:00
|
|
|
public:
|
2021-08-07 20:51:18 +02:00
|
|
|
Test64BitPortability() : TestFixture("Test64BitPortability") {}
|
2011-07-06 08:55:17 +02:00
|
|
|
|
|
|
|
private:
|
2023-05-02 11:48:24 +02:00
|
|
|
const Settings settings = settingsBuilder().severity(Severity::portability).library("std.cfg").build();
|
2011-07-06 08:55:17 +02:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
void run() override {
|
2011-07-06 08:55:17 +02:00
|
|
|
TEST_CASE(novardecl);
|
|
|
|
TEST_CASE(functionpar);
|
|
|
|
TEST_CASE(structmember);
|
2011-07-07 15:14:33 +02:00
|
|
|
TEST_CASE(ptrcompare);
|
2011-09-04 13:03:29 +02:00
|
|
|
TEST_CASE(ptrarithmetic);
|
2012-04-26 13:39:19 +02:00
|
|
|
TEST_CASE(returnIssues);
|
2021-08-23 08:51:54 +02:00
|
|
|
TEST_CASE(assignment);
|
2011-07-06 08:55:17 +02:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:34:39 +01:00
|
|
|
#define check(code) check_(code, __FILE__, __LINE__)
|
|
|
|
void check_(const char code[], const char* file, int line) {
|
2011-07-06 08:55:17 +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, "test.cpp"), file, line);
|
2011-07-06 08:55:17 +02:00
|
|
|
|
|
|
|
// Check char variable usage..
|
|
|
|
Check64BitPortability check64BitPortability(&tokenizer, &settings, this);
|
|
|
|
check64BitPortability.pointerassignment();
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:51:54 +02:00
|
|
|
void assignment() {
|
|
|
|
// #8631
|
|
|
|
check("using CharArray = char[16];\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" CharArray foo = \"\";\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2023-02-17 07:17:37 +01:00
|
|
|
|
|
|
|
check("struct T { std::vector<int>*a[2][2]; };\n" // #11560
|
|
|
|
"void f(T& t, int i, int j) {\n"
|
|
|
|
" t.a[i][j] = new std::vector<int>;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-08-23 08:51:54 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void novardecl() {
|
2011-07-06 08:55:17 +02:00
|
|
|
// if the variable declarations can't be seen then skip the warning
|
|
|
|
check("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" a = p;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2011-07-06 08:55:17 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void functionpar() {
|
2011-07-06 08:55:17 +02:00
|
|
|
check("int foo(int *p)\n"
|
|
|
|
"{\n"
|
|
|
|
" int a = p;\n"
|
|
|
|
" return a + 4;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2012-07-08 14:28:17 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning a pointer to an integer is not portable.\n", errout.str());
|
2011-07-06 08:55:17 +02:00
|
|
|
|
|
|
|
check("int foo(int p[])\n"
|
|
|
|
"{\n"
|
|
|
|
" int a = p;\n"
|
|
|
|
" return a + 4;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2012-07-08 14:28:17 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning a pointer to an integer is not portable.\n", errout.str());
|
2011-07-06 08:55:17 +02:00
|
|
|
|
2011-07-16 17:08:03 +02:00
|
|
|
check("int foo(int p[])\n"
|
|
|
|
"{\n"
|
|
|
|
" int *a = p;\n"
|
|
|
|
" return a;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2012-04-26 13:39:19 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout.str());
|
2011-07-16 17:08:03 +02:00
|
|
|
|
2011-07-06 08:55:17 +02:00
|
|
|
check("void foo(int x)\n"
|
|
|
|
"{\n"
|
|
|
|
" int *p = x;\n"
|
|
|
|
" *p = 0;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2012-07-08 14:28:17 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an integer to a pointer is not portable.\n", errout.str());
|
2013-03-17 17:25:57 +01:00
|
|
|
|
|
|
|
check("int f(const char *p) {\n" // #4659
|
|
|
|
" return 6 + p[2] * 256;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-01-02 21:07:02 +01:00
|
|
|
|
|
|
|
check("int foo(int *p) {\n" // #6096
|
|
|
|
" bool a = p;\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2020-12-24 19:57:02 +01:00
|
|
|
|
|
|
|
check("std::array<int,2> f();\n"
|
|
|
|
"void g() {\n"
|
|
|
|
" std::array<int, 2> a = f();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("std::array<int,2> f(int x);\n"
|
|
|
|
"void g(int i) {\n"
|
|
|
|
" std::array<int, 2> a = f(i);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("typedef std::array<int, 2> Array;\n"
|
|
|
|
"Array f(int x);\n"
|
|
|
|
"void g(int i) {\n"
|
|
|
|
" Array a = f(i);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("typedef std::array<int, 2> Array;\n"
|
|
|
|
"Array f();\n"
|
|
|
|
"void g(int i) {\n"
|
|
|
|
" Array a = f();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-02-02 19:30:49 +01:00
|
|
|
|
|
|
|
check("struct S {\n" // #9951
|
|
|
|
" enum E { E0 };\n"
|
|
|
|
" std::array<double, 1> g(S::E);\n"
|
|
|
|
"};\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" std::array<double, 1> a = S::g(S::E::E0);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2023-01-26 22:09:55 +01:00
|
|
|
|
|
|
|
check("char* f(char* p) {\n"
|
|
|
|
" return p ? p : 0;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-07-06 08:55:17 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void structmember() {
|
2014-10-02 14:57:09 +02:00
|
|
|
check("struct Foo { int *p; };\n"
|
2011-07-06 08:55:17 +02:00
|
|
|
"void f(struct Foo *foo) {\n"
|
|
|
|
" int i = foo->p;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2014-10-02 14:57:09 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning a pointer to an integer is not portable.\n", errout.str());
|
2022-02-08 16:12:35 +01:00
|
|
|
|
|
|
|
check("struct S {\n" // #10145
|
|
|
|
" enum class E { e1, e2 };\n"
|
|
|
|
" E e;\n"
|
|
|
|
" char* e1;\n"
|
|
|
|
"};\n"
|
|
|
|
"void f(S& s) {\n"
|
|
|
|
" s.e = S::E::e1;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-07-06 08:55:17 +02:00
|
|
|
}
|
2011-07-07 15:14:33 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void ptrcompare() {
|
2011-07-07 15:14:33 +02:00
|
|
|
// Ticket #2892
|
|
|
|
check("void foo(int *p) {\n"
|
|
|
|
" int a = (p != NULL);\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2011-07-07 15:14:33 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2011-09-04 13:03:29 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void ptrarithmetic() {
|
2011-09-04 13:03:29 +02:00
|
|
|
// #3073
|
|
|
|
check("void foo(int *p) {\n"
|
|
|
|
" int x = 10;\n"
|
|
|
|
" int *a = p + x;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2011-09-04 13:03:29 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void foo(int *p) {\n"
|
|
|
|
" int x = 10;\n"
|
|
|
|
" int *a = x + p;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2011-09-04 13:03:29 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void foo(int *p) {\n"
|
|
|
|
" int x = 10;\n"
|
|
|
|
" int *a = x * x;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2015-12-30 19:59:23 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an integer to a pointer is not portable.\n", errout.str());
|
2011-10-12 19:01:44 +02:00
|
|
|
|
|
|
|
check("void foo(int *start, int *end) {\n"
|
|
|
|
" int len;\n"
|
|
|
|
" int len = end + 10 - start;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2011-10-12 19:01:44 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-09-04 13:03:29 +02:00
|
|
|
}
|
2012-04-26 13:39:19 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void returnIssues() {
|
2012-04-26 13:39:19 +02:00
|
|
|
check("void* foo(int i) {\n"
|
|
|
|
" return i;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2012-07-08 14:28:17 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an integer in a function with pointer return type is not portable.\n", errout.str());
|
2012-04-26 13:39:19 +02:00
|
|
|
|
|
|
|
check("void* foo(int* i) {\n"
|
|
|
|
" return i;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2013-02-04 19:02:42 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void* foo() {\n"
|
|
|
|
" return 0;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2012-04-26 13:39:19 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("int foo(int i) {\n"
|
|
|
|
" return i;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2012-04-26 13:39:19 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2016-07-29 21:53:58 +02:00
|
|
|
check("struct Foo {};\n"
|
|
|
|
"\n"
|
|
|
|
"int* dostuff(Foo foo) {\n"
|
|
|
|
" return foo;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2012-04-26 13:39:19 +02:00
|
|
|
check("int foo(char* c) {\n"
|
|
|
|
" return c;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2012-04-26 13:39:19 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout.str());
|
|
|
|
|
2013-02-04 18:14:52 +01:00
|
|
|
check("int foo(char* c) {\n"
|
|
|
|
" return 1+c;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2013-02-04 18:14:52 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout.str());
|
|
|
|
|
2012-04-26 13:39:19 +02:00
|
|
|
check("std::string foo(char* c) {\n"
|
|
|
|
" return c;\n"
|
2013-03-19 09:18:58 +01:00
|
|
|
"}");
|
2012-04-26 13:39:19 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-02-04 18:14:52 +01:00
|
|
|
|
|
|
|
check("int foo(char *a, char *b) {\n" // #4486
|
|
|
|
" return a + 1 - b;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-03-12 06:49:13 +01:00
|
|
|
|
|
|
|
check("struct s {\n" // 4642
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"int func(struct s *p) {\n"
|
|
|
|
" return 1 + p->i;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-10-08 06:36:45 +02:00
|
|
|
|
|
|
|
check("static void __iomem *f(unsigned int port_no) {\n"
|
|
|
|
" void __iomem *mmio = hpriv->mmio;\n"
|
|
|
|
" return mmio + (port_no * 0x80);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-12-31 18:53:07 +01:00
|
|
|
|
2016-11-27 11:40:42 +01:00
|
|
|
// #7247: don't check return statements in nested functions..
|
2015-12-31 18:53:07 +01:00
|
|
|
check("int foo() {\n"
|
|
|
|
" struct {\n"
|
|
|
|
" const char * name() { return \"abc\"; }\n"
|
|
|
|
" } table;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2016-05-04 13:23:50 +02:00
|
|
|
|
|
|
|
// #7451: Lambdas
|
|
|
|
check("const int* test(std::vector<int> outputs, const std::string& text) {\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
" auto it = std::find_if(outputs.begin(), outputs.end(),\n"
|
2016-05-04 13:23:50 +02:00
|
|
|
" [&](int ele) { return \"test\" == text; });\n"
|
|
|
|
" return nullptr;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2023-11-08 13:12:50 +01:00
|
|
|
|
|
|
|
check("struct S {\n" // #12159
|
|
|
|
" std::future<int> f() const {\n"
|
|
|
|
" return {};\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n"
|
|
|
|
"int g() {\n"
|
|
|
|
" std::shared_ptr<S> s = std::make_shared<S>();\n"
|
|
|
|
" auto x = s->f();\n"
|
|
|
|
" return x.get();\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-04-26 13:39:19 +02:00
|
|
|
}
|
2011-07-06 08:55:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(Test64BitPortability)
|