forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test64bit.cpp
259 lines (218 loc) · 8.16 KB
/
test64bit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2019 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 <http://www.gnu.org/licenses/>.
*/
#include "check64bit.h"
#include "settings.h"
#include "testsuite.h"
#include "tokenize.h"
class Test64BitPortability : public TestFixture {
public:
Test64BitPortability() : TestFixture("Test64BitPortability") {
}
private:
Settings settings;
void run() OVERRIDE {
settings.addEnabled("portability");
TEST_CASE(novardecl);
TEST_CASE(functionpar);
TEST_CASE(structmember);
TEST_CASE(ptrcompare);
TEST_CASE(ptrarithmetic);
TEST_CASE(returnIssues);
}
void check(const char code[]) {
// Clear the error buffer..
errout.str("");
// Tokenize..
Tokenizer tokenizer(&settings, this);
LOAD_LIB_2(settings.library, "std.cfg");
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Check char variable usage..
Check64BitPortability check64BitPortability(&tokenizer, &settings, this);
check64BitPortability.pointerassignment();
}
void novardecl() {
// if the variable declarations can't be seen then skip the warning
check("void foo()\n"
"{\n"
" a = p;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void functionpar() {
check("int foo(int *p)\n"
"{\n"
" int a = p;\n"
" return a + 4;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning a pointer to an integer is not portable.\n", errout.str());
check("int foo(int p[])\n"
"{\n"
" int a = p;\n"
" return a + 4;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning a pointer to an integer is not portable.\n", errout.str());
check("int foo(int p[])\n"
"{\n"
" int *a = p;\n"
" return a;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout.str());
check("void foo(int x)\n"
"{\n"
" int *p = x;\n"
" *p = 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an integer to a pointer is not portable.\n", errout.str());
check("int f(const char *p) {\n" // #4659
" return 6 + p[2] * 256;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int foo(int *p) {\n" // #6096
" bool a = p;\n"
" return a;\n"
"}");
ASSERT_EQUALS("", errout.str());
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());
}
void structmember() {
check("struct Foo { int *p; };\n"
"void f(struct Foo *foo) {\n"
" int i = foo->p;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning a pointer to an integer is not portable.\n", errout.str());
}
void ptrcompare() {
// Ticket #2892
check("void foo(int *p) {\n"
" int a = (p != NULL);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void ptrarithmetic() {
// #3073
check("void foo(int *p) {\n"
" int x = 10;\n"
" int *a = p + x;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo(int *p) {\n"
" int x = 10;\n"
" int *a = x + p;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo(int *p) {\n"
" int x = 10;\n"
" int *a = x * x;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an integer to a pointer is not portable.\n", errout.str());
check("void foo(int *start, int *end) {\n"
" int len;\n"
" int len = end + 10 - start;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void returnIssues() {
check("void* foo(int i) {\n"
" return i;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an integer in a function with pointer return type is not portable.\n", errout.str());
check("void* foo(int* i) {\n"
" return i;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void* foo() {\n"
" return 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int foo(int i) {\n"
" return i;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct Foo {};\n"
"\n"
"int* dostuff(Foo foo) {\n"
" return foo;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int foo(char* c) {\n"
" return c;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout.str());
check("int foo(char* c) {\n"
" return 1+c;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (portability) Returning an address value in a function with integer return type is not portable.\n", errout.str());
check("std::string foo(char* c) {\n"
" return c;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("int foo(char *a, char *b) {\n" // #4486
" return a + 1 - b;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct s {\n" // 4642
" int i;\n"
"};\n"
"int func(struct s *p) {\n"
" return 1 + p->i;\n"
"}");
ASSERT_EQUALS("", errout.str());
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());
// #7247: don't check return statements in nested functions..
check("int foo() {\n"
" struct {\n"
" const char * name() { return \"abc\"; }\n"
" } table;\n"
"}");
ASSERT_EQUALS("", errout.str());
// #7451: Lambdas
check("const int* test(std::vector<int> outputs, const std::string& text) {\n"
" auto it = std::find_if(outputs.begin(), outputs.end(), \n"
" [&](int ele) { return \"test\" == text; });\n"
" return nullptr;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(Test64BitPortability)