blob: e79a9a64479967bd91afdf1d253cc12008b3953f [file] [log] [blame]
Dan Albert2fbb1b62014-10-08 11:21:32 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
George Burgess IVc7bd90f2017-08-23 17:32:48 -070017/*
18 * If this test fails, you can see the compiler's output by erasing a few args from the failing
19 * command. Specifically, delete everything before the path/to/the/compiler, then delete the first
20 * arg after the path/to/the/compiler. For example, given the following command:
21 *
22 * bionic/tests/file-check-cxx out/host/linux-x86/bin/FileCheck \
23 * prebuilts/clang/host/linux-x86/clang-4053586/bin/clang++ CLANG -I bionic/tests -I ...
24 *
Elliott Hughes0d1a8a52018-07-24 19:36:51 +000025 * If you delete everything before clang++ and delete "CLANG", then you'll end up with:
George Burgess IVc7bd90f2017-08-23 17:32:48 -070026 *
27 * prebuilts/clang/host/linux-x86/clang-4053586/bin/clang++ -I bionic/tests -I ...
28 *
29 * Which is the command that FileCheck executes.
30 */
31
Dan Albert2fbb1b62014-10-08 11:21:32 -070032#undef _FORTIFY_SOURCE
33#define _FORTIFY_SOURCE 2
Yabin Cui20f22682015-03-03 20:27:58 -080034#include <fcntl.h>
35#include <netinet/in.h>
36#include <poll.h>
37#include <stdarg.h>
Dan Albert2fbb1b62014-10-08 11:21:32 -070038#include <stdio.h>
Daniel Micayafdd1542015-07-20 21:37:29 -040039#include <stdlib.h>
Yabin Cui20f22682015-03-03 20:27:58 -080040#include <string.h>
41#include <sys/socket.h>
42#include <sys/stat.h>
43#include <time.h>
44#include <unistd.h>
Dan Albert2fbb1b62014-10-08 11:21:32 -070045
46void test_sprintf() {
47 char buf[4];
48
49 // NOLINTNEXTLINE(whitespace/line_length)
George Burgess IV7cc779f2017-02-09 00:00:31 -080050 // CLANG: error: call to unavailable function 'sprintf': format string will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -080051 sprintf(buf, "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070052
Elliott Hughes0d1a8a52018-07-24 19:36:51 +000053 // TODO: clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080054 sprintf(buf, "%s", "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070055}
56
57void test_snprintf() {
58 char buf[4];
59
60 // NOLINTNEXTLINE(whitespace/line_length)
George Burgess IV7cc779f2017-02-09 00:00:31 -080061 // CLANG: error: call to unavailable function 'snprintf': format string will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -080062 snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070063
Elliott Hughes0d1a8a52018-07-24 19:36:51 +000064 // TODO: clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080065 snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070066
Elliott Hughes0d1a8a52018-07-24 19:36:51 +000067 // TODO: clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080068 snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070069
Elliott Hughes0d1a8a52018-07-24 19:36:51 +000070 // TODO: clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080071 snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf)
72}
73
74void test_memcpy() {
75 char buf[4];
76
George Burgess IVb6300462017-07-31 21:29:42 -070077 // CLANG: error: 'memcpy' called with size bigger than buffer
George Burgess IV7cc779f2017-02-09 00:00:31 -080078 memcpy(buf, "foobar", sizeof("foobar") + 100);
Yabin Cui20f22682015-03-03 20:27:58 -080079}
80
81void test_memmove() {
82 char buf[4];
83
George Burgess IVb6300462017-07-31 21:29:42 -070084 // CLANG: error: 'memmove' called with size bigger than buffer
Yabin Cui20f22682015-03-03 20:27:58 -080085 memmove(buf, "foobar", sizeof("foobar"));
86}
87
88void test_memset() {
89 char buf[4];
90
George Burgess IVb6300462017-07-31 21:29:42 -070091 // CLANG: error: 'memset' called with size bigger than buffer
Yabin Cui20f22682015-03-03 20:27:58 -080092 memset(buf, 0, 6);
93}
94
95void test_strcpy() {
96 char buf[4];
97
George Burgess IVb6300462017-07-31 21:29:42 -070098 // CLANG: error: 'strcpy' called with string bigger than buffer
Yabin Cui20f22682015-03-03 20:27:58 -080099 strcpy(buf, "foobar"); // NOLINT(runtime/printf)
George Burgess IVb6300462017-07-31 21:29:42 -0700100
George Burgess IVb6300462017-07-31 21:29:42 -0700101 // CLANG: error: 'strcpy' called with string bigger than buffer
102 strcpy(buf, "quux");
Yabin Cui20f22682015-03-03 20:27:58 -0800103}
104
105void test_stpcpy() {
106 char buf[4];
107
George Burgess IVb6300462017-07-31 21:29:42 -0700108 // CLANG: error: 'stpcpy' called with string bigger than buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800109 stpcpy(buf, "foobar");
George Burgess IVb6300462017-07-31 21:29:42 -0700110
George Burgess IVb6300462017-07-31 21:29:42 -0700111 // CLANG: error: 'stpcpy' called with string bigger than buffer
112 stpcpy(buf, "quux");
Yabin Cui20f22682015-03-03 20:27:58 -0800113}
114
115void test_strncpy() {
116 char buf[4];
117
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000118 // TODO: clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -0800119 strncpy(buf, "foobar", sizeof("foobar"));
120}
121
122void test_strcat() {
123 char buf[4] = "";
124
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000125 // TODO: clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -0800126 strcat(buf, "foobar"); // NOLINT(runtime/printf)
127}
128
129void test_strncat() {
130 char buf[4] = "";
131
Elliott Hughes0d1a8a52018-07-24 19:36:51 +0000132 // TODO: clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -0800133 strncat(buf, "foobar", sizeof("foobar"));
134}
135
136void test_vsprintf(const char* fmt, ...) {
137 va_list va;
138 char buf[4];
139 va_start(va, fmt);
140
Yabin Cui20f22682015-03-03 20:27:58 -0800141 // clang should emit a warning, but doesn't
142 vsprintf(buf, "foobar", va);
143 va_end(va);
144}
145
146void test_vsnprintf(const char* fmt, ...) {
147 va_list va;
148 char buf[4];
149 va_start(va, fmt);
150
Yabin Cui20f22682015-03-03 20:27:58 -0800151 // clang should emit a warning, but doesn't
152 vsnprintf(buf, 5, "foobar", va); // NOLINT(runtime/printf)
153
154 va_end(va);
155}
156
157void test_fgets() {
158 char buf[4];
159
George Burgess IV23dbf822017-07-31 21:23:34 -0700160 // CLANG: error: in call to 'fgets', size should not be negative
Yabin Cui20f22682015-03-03 20:27:58 -0800161 fgets(buf, -1, stdin);
162
George Burgess IV23dbf822017-07-31 21:23:34 -0700163 // CLANG: error: in call to 'fgets', size is larger than the destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800164 fgets(buf, 6, stdin);
165}
166
167void test_recvfrom() {
168 char buf[4];
169 sockaddr_in addr;
170
George Burgess IV54f5d832017-07-31 21:21:10 -0700171 // CLANG: error: 'recvfrom' called with size bigger than buffer
Yi Kong32bc0fc2018-08-02 17:31:13 -0700172 recvfrom(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), nullptr);
Yabin Cui20f22682015-03-03 20:27:58 -0800173}
174
George Burgess IV54f5d832017-07-31 21:21:10 -0700175void test_recv() {
176 char buf[4] = {0};
177
George Burgess IV54f5d832017-07-31 21:21:10 -0700178 // CLANG: error: 'recv' called with size bigger than buffer
179 recv(0, buf, 6, 0);
180}
181
Yabin Cui20f22682015-03-03 20:27:58 -0800182void test_umask() {
George Burgess IV52dde5f2017-07-31 21:16:05 -0700183 // CLANG: error: 'umask' called with invalid mode
Yabin Cui20f22682015-03-03 20:27:58 -0800184 umask(01777);
185}
186
187void test_read() {
188 char buf[4];
George Burgess IV16c17392017-07-31 21:30:47 -0700189 // CLANG: error: in call to 'read', 'count' bytes overflows the given object
Yabin Cui20f22682015-03-03 20:27:58 -0800190 read(0, buf, 6);
191}
192
193void test_open() {
Elliott Hughesb115aef2017-08-04 09:34:19 -0700194 // CLANG: error: 'open' called with O_CREAT or O_TMPFILE, but missing mode
Yabin Cui20f22682015-03-03 20:27:58 -0800195 open("/dev/null", O_CREAT);
196
Elliott Hughesb115aef2017-08-04 09:34:19 -0700197 // CLANG: error: 'open' called with O_CREAT or O_TMPFILE, but missing mode
198 open("/dev/null", O_TMPFILE);
199
George Burgess IV7cc779f2017-02-09 00:00:31 -0800200 // CLANG: error: call to unavailable function 'open': too many arguments
Yabin Cui20f22682015-03-03 20:27:58 -0800201 open("/dev/null", O_CREAT, 0, 0);
George Burgess IV4e37d532017-08-03 17:11:35 -0700202
Elliott Hughesb115aef2017-08-04 09:34:19 -0700203 // CLANG: error: call to unavailable function 'open': too many arguments
204 open("/dev/null", O_TMPFILE, 0, 0);
205
George Burgess IV4e37d532017-08-03 17:11:35 -0700206 // CLANG: warning: 'open' has superfluous mode bits; missing O_CREAT?
207 open("/dev/null", O_RDONLY, 0644);
208
209 // CLANG: warning: 'open' has superfluous mode bits; missing O_CREAT?
210 open("/dev/null", O_DIRECTORY, 0644);
Yabin Cui20f22682015-03-03 20:27:58 -0800211}
212
213void test_poll() {
214 pollfd fds[1];
George Burgess IV52dde5f2017-07-31 21:16:05 -0700215 // CLANG: error: in call to 'poll', fd_count is larger than the given buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800216 poll(fds, 2, 0);
217}
218
219void test_ppoll() {
220 pollfd fds[1];
221 timespec timeout;
George Burgess IV52dde5f2017-07-31 21:16:05 -0700222 // CLANG: error: in call to 'ppoll', fd_count is larger than the given buffer
Elliott Hughesb83bf142018-03-22 11:01:25 -0700223 ppoll(fds, 2, &timeout, nullptr);
224}
225
226void test_ppoll64() {
227 pollfd fds[1];
228 timespec timeout;
229 // NOLINTNEXTLINE(whitespace/line_length)
230 // CLANG: error: in call to 'ppoll64', fd_count is larger than the given buffer
231 ppoll64(fds, 2, &timeout, nullptr);
Dan Albert2fbb1b62014-10-08 11:21:32 -0700232}
Daniel Micayfed26592015-07-18 13:55:51 -0400233
234void test_fread_overflow() {
235 char buf[4];
George Burgess IV23dbf822017-07-31 21:23:34 -0700236 // CLANG: error: in call to 'fread', size * count overflows
Daniel Micayfed26592015-07-18 13:55:51 -0400237 fread(buf, 2, (size_t)-1, stdin);
238}
239
240void test_fread_too_big() {
241 char buf[4];
242 // NOLINTNEXTLINE(whitespace/line_length)
George Burgess IV23dbf822017-07-31 21:23:34 -0700243 // CLANG: error: in call to 'fread', size * count is too large for the given buffer
Daniel Micayfed26592015-07-18 13:55:51 -0400244 fread(buf, 1, 5, stdin);
245}
246
247void test_fwrite_overflow() {
Daniel Micayafdd1542015-07-20 21:37:29 -0400248 char buf[4] = {0};
George Burgess IV23dbf822017-07-31 21:23:34 -0700249 // CLANG: error: in call to 'fwrite', size * count overflows
Daniel Micayfed26592015-07-18 13:55:51 -0400250 fwrite(buf, 2, (size_t)-1, stdout);
251}
252
253void test_fwrite_too_big() {
254 char buf[4] = {0};
255 // NOLINTNEXTLINE(whitespace/line_length)
George Burgess IV23dbf822017-07-31 21:23:34 -0700256 // CLANG: error: in call to 'fwrite', size * count is too large for the given buffer
Daniel Micayfed26592015-07-18 13:55:51 -0400257 fwrite(buf, 1, 5, stdout);
258}
Daniel Micay9101b002015-05-20 15:31:26 -0400259
260void test_getcwd() {
261 char buf[4];
George Burgess IV16c17392017-07-31 21:30:47 -0700262 // CLANG: error: in call to 'getcwd', 'size' bytes overflows the given object
Daniel Micay9101b002015-05-20 15:31:26 -0400263 getcwd(buf, 5);
264}
Daniel Micayafdd1542015-07-20 21:37:29 -0400265
266void test_pwrite64_size() {
267 char buf[4] = {0};
George Burgess IV16c17392017-07-31 21:30:47 -0700268 // CLANG: error: in call to 'pwrite64', 'count' bytes overflows the given object
Daniel Micayafdd1542015-07-20 21:37:29 -0400269 pwrite64(STDOUT_FILENO, buf, 5, 0);
270}
271
George Burgess IV7cc779f2017-02-09 00:00:31 -0800272void test_pwrite64_too_big_malloc() {
Daniel Micayafdd1542015-07-20 21:37:29 -0400273 void *buf = calloc(atoi("5"), 1);
George Burgess IV7cc779f2017-02-09 00:00:31 -0800274 // clang should emit a warning, but probably never will.
275 pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0);
276}
277
278void test_pwrite64_too_big() {
279 char buf[4] = {0};
George Burgess IV16c17392017-07-31 21:30:47 -0700280 // CLANG: error: in call to 'pwrite64', 'count' must be <= SSIZE_MAX
Daniel Micayafdd1542015-07-20 21:37:29 -0400281 pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0);
282}
283
284void test_write_size() {
285 char buf[4] = {0};
George Burgess IV16c17392017-07-31 21:30:47 -0700286 // CLANG: error: in call to 'write', 'count' bytes overflows the given object
Daniel Micayafdd1542015-07-20 21:37:29 -0400287 write(STDOUT_FILENO, buf, 5);
288}
George Burgess IV7cc779f2017-02-09 00:00:31 -0800289
290void test_memset_args_flipped() {
291 char from[4] = {0};
292 // NOLINTNEXTLINE(whitespace/line_length)
George Burgess IVb6300462017-07-31 21:29:42 -0700293 // CLANG: 'memset' will set 0 bytes; maybe the arguments got flipped?
Stephen Hinescd659d42018-09-20 22:58:01 -0700294#pragma clang diagnostic push
295#pragma clang diagnostic ignored "-Wmemset-transposed-args"
George Burgess IV7cc779f2017-02-09 00:00:31 -0800296 memset(from, sizeof(from), 0);
Stephen Hinescd659d42018-09-20 22:58:01 -0700297#pragma clang diagnostic pop
George Burgess IV7cc779f2017-02-09 00:00:31 -0800298}
Daniel Micay95b59c52017-02-13 17:27:59 -0800299
300void test_sendto() {
301 char buf[4] = {0};
302 sockaddr_in addr;
303
George Burgess IV54f5d832017-07-31 21:21:10 -0700304 // CLANG: error: 'sendto' called with size bigger than buffer
Daniel Micay95b59c52017-02-13 17:27:59 -0800305 sendto(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr_in));
306}
307
308void test_send() {
309 char buf[4] = {0};
310
George Burgess IV54f5d832017-07-31 21:21:10 -0700311 // CLANG: error: 'send' called with size bigger than buffer
Daniel Micay95b59c52017-02-13 17:27:59 -0800312 send(0, buf, 6, 0);
313}
George Burgess IV54f5d832017-07-31 21:21:10 -0700314
315void test_realpath() {
316 char buf[4] = {0};
317 // NOLINTNEXTLINE(whitespace/line_length)
George Burgess IV54f5d832017-07-31 21:21:10 -0700318 // CLANG: error: 'realpath' output parameter must be NULL or a pointer to a buffer with >= PATH_MAX bytes
319 realpath(".", buf);
320
321 // This is fine.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700322 realpath(".", nullptr);
George Burgess IV54f5d832017-07-31 21:21:10 -0700323
George Burgess IV95bd4882017-08-14 14:48:55 -0700324 char bigbuf[PATH_MAX];
325 // CLANG: error: 'realpath': NULL path is never correct; flipped arguments?
Yi Kong32bc0fc2018-08-02 17:31:13 -0700326 realpath(nullptr, bigbuf);
George Burgess IV54f5d832017-07-31 21:21:10 -0700327}