blob: 072006ea5bdfe17bf8d3a6d0e2b89b45e4b65b2e [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
17#undef _FORTIFY_SOURCE
18#define _FORTIFY_SOURCE 2
Yabin Cui20f22682015-03-03 20:27:58 -080019#include <fcntl.h>
20#include <netinet/in.h>
21#include <poll.h>
22#include <stdarg.h>
Dan Albert2fbb1b62014-10-08 11:21:32 -070023#include <stdio.h>
Yabin Cui20f22682015-03-03 20:27:58 -080024#include <string.h>
25#include <sys/socket.h>
26#include <sys/stat.h>
27#include <time.h>
28#include <unistd.h>
Dan Albert2fbb1b62014-10-08 11:21:32 -070029
30void test_sprintf() {
31 char buf[4];
32
33 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070034 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070035 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080036 sprintf(buf, "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070037
38 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070039 // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070040 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080041 sprintf(buf, "%s", "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070042}
43
44void test_snprintf() {
45 char buf[4];
46
47 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070048 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070049 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080050 snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070051
52 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070053 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070054 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080055 snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070056
57 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070058 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070059 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080060 snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf)
Dan Albert2fbb1b62014-10-08 11:21:32 -070061
62 // NOLINTNEXTLINE(whitespace/line_length)
Elliott Hughesb4b15c62014-10-08 13:21:29 -070063 // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer
Dan Albert2fbb1b62014-10-08 11:21:32 -070064 // clang should emit a warning, but doesn't
Yabin Cui20f22682015-03-03 20:27:58 -080065 snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf)
66}
67
68void test_memcpy() {
69 char buf[4];
70
71 // NOLINTNEXTLINE(whitespace/line_length)
72 // GCC: warning: call to void* __builtin___memcpy_chk(void*, const void*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
73 // clang should emit a warning, but doesn't
74 memcpy(buf, "foobar", sizeof("foobar"));
75}
76
77void test_memmove() {
78 char buf[4];
79
80 // NOLINTNEXTLINE(whitespace/line_length)
81 // GCC: warning: call to void* __builtin___memmove_chk(void*, const void*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
82 // clang should emit a warning, but doesn't
83 memmove(buf, "foobar", sizeof("foobar"));
84}
85
86void test_memset() {
87 char buf[4];
88
89 // NOLINTNEXTLINE(whitespace/line_length)
90 // GCC: warning: call to void* __builtin___memset_chk(void*, int, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
91 // clang should emit a warning, but doesn't
92 memset(buf, 0, 6);
93}
94
95void test_strcpy() {
96 char buf[4];
97
98 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuif3bd3052015-03-04 21:43:14 -080099 // GCC: warning: call to {{(char\* __builtin___strcpy_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800100 // clang should emit a warning, but doesn't
101 strcpy(buf, "foobar"); // NOLINT(runtime/printf)
102}
103
104void test_stpcpy() {
105 char buf[4];
106
107 // NOLINTNEXTLINE(whitespace/line_length)
108 // GCC: warning: call to char* __builtin___stpcpy_chk(char*, const char*, {{(long )?}}unsigned int) will always overflow destination buffer
109 // clang should emit a warning, but doesn't
110 stpcpy(buf, "foobar");
111}
112
113void test_strncpy() {
114 char buf[4];
115
116 // NOLINTNEXTLINE(whitespace/line_length)
117 // GCC: warning: call to char* __builtin___strncpy_chk(char*, const char*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer
118 // clang should emit a warning, but doesn't
119 strncpy(buf, "foobar", sizeof("foobar"));
120}
121
122void test_strcat() {
123 char buf[4] = "";
124
125 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuif3bd3052015-03-04 21:43:14 -0800126 // GCC: warning: call to {{(char\* __builtin___strcat_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800127 // clang should emit a warning, but doesn't
128 strcat(buf, "foobar"); // NOLINT(runtime/printf)
129}
130
131void test_strncat() {
132 char buf[4] = "";
133
134 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuif3bd3052015-03-04 21:43:14 -0800135 // GCC: warning: call to {{(char\* __builtin___strcat_chk\(char\*, const char\*, unsigned int\))|(void\* __builtin___memcpy_chk\(void\*, const void\*, (long )?unsigned int, (long )?unsigned int\))}} will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800136 // gcc output warning with __builtin___strcat_chk for __builtin___strncat_chk.
137 // clang should emit a warning, but doesn't
138 strncat(buf, "foobar", sizeof("foobar"));
139}
140
141void test_vsprintf(const char* fmt, ...) {
142 va_list va;
143 char buf[4];
144 va_start(va, fmt);
145
146 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuid9647592015-03-05 00:39:09 -0800147 // GCC: warning: call to int __builtin___vsprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, {{(__va_list)|(void\*)|(char\*)|(__va_list_tag\*)}}) will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800148 // clang should emit a warning, but doesn't
149 vsprintf(buf, "foobar", va);
150 va_end(va);
151}
152
153void test_vsnprintf(const char* fmt, ...) {
154 va_list va;
155 char buf[4];
156 va_start(va, fmt);
157
158 // NOLINTNEXTLINE(whitespace/line_length)
Yabin Cuid9647592015-03-05 00:39:09 -0800159 // GCC: warning: call to int __builtin___vsnprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, {{(__va_list)|(void\*)|(char\*)|(__va_list_tag\*)}}) will always overflow destination buffer
Yabin Cui20f22682015-03-03 20:27:58 -0800160 // clang should emit a warning, but doesn't
161 vsnprintf(buf, 5, "foobar", va); // NOLINT(runtime/printf)
162
163 va_end(va);
164}
165
166void test_fgets() {
167 char buf[4];
168
169 // NOLINTNEXTLINE(whitespace/line_length)
170 // GCC: error: call to '__fgets_too_small_error' declared with attribute error: fgets called with size less than zero
171 // clang should emit a warning, but doesn't
172 fgets(buf, -1, stdin);
173
174 // NOLINTNEXTLINE(whitespace/line_length)
175 // GCC: error: call to '__fgets_too_big_error' declared with attribute error: fgets called with size bigger than buffer
176 // clang should emit a warning, but doesn't
177 fgets(buf, 6, stdin);
178}
179
180void test_recvfrom() {
181 char buf[4];
182 sockaddr_in addr;
183
184 // NOLINTNEXTLINE(whitespace/line_length)
185 // GCC: error: call to '__recvfrom_error' declared with attribute error: recvfrom called with size bigger than buffer
186 // clang should emit a warning, but doesn't
187 recvfrom(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), NULL);
188}
189
190void test_umask() {
191 // NOLINTNEXTLINE(whitespace/line_length)
192 // GCC: error: call to '__umask_invalid_mode' declared with attribute error: umask called with invalid mode
193 // clang should emit a warning, but doesn't
194 umask(01777);
195}
196
197void test_read() {
198 char buf[4];
199 // NOLINTNEXTLINE(whitespace/line_length)
200 // GCC: error: call to '__read_dest_size_error' declared with attribute error: read called with size bigger than destination
201 // clang should emit a warning, but doesn't
202 read(0, buf, 6);
203}
204
205void test_open() {
206 // NOLINTNEXTLINE(whitespace/line_length)
207 // GCC: error: call to '__creat_missing_mode' declared with attribute error: called with O_CREAT, but missing mode
208 // clang should emit a warning, but doesn't
209 open("/dev/null", O_CREAT);
210
211 // NOLINTNEXTLINE(whitespace/line_length)
212 // GCC: error: call to '__creat_too_many_args' declared with attribute error: too many arguments
213 // clang should emit a warning, but doesn't
214 open("/dev/null", O_CREAT, 0, 0);
215}
216
217void test_poll() {
218 pollfd fds[1];
219 // NOLINTNEXTLINE(whitespace/line_length)
220 // GCC: error: call to '__poll_too_small_error' declared with attribute error: poll: pollfd array smaller than fd count
221 // clang should emit a warning, but doesn't
222 poll(fds, 2, 0);
223}
224
225void test_ppoll() {
226 pollfd fds[1];
227 timespec timeout;
228 // NOLINTNEXTLINE(whitespace/line_length)
229 // GCC: error: call to '__ppoll_too_small_error' declared with attribute error: ppoll: pollfd array smaller than fd count
230 // clang should emit a warning, but doesn't
231 ppoll(fds, 2, &timeout, NULL);
Dan Albert2fbb1b62014-10-08 11:21:32 -0700232}
Daniel Micayfed26592015-07-18 13:55:51 -0400233
234void test_fread_overflow() {
235 char buf[4];
236 // NOLINTNEXTLINE(whitespace/line_length)
237 // GCC: error: call to '__fread_overflow' declared with attribute error: fread called with overflowing size * count
238 // clang should emit a warning, but doesn't
239 fread(buf, 2, (size_t)-1, stdin);
240}
241
242void test_fread_too_big() {
243 char buf[4];
244 // NOLINTNEXTLINE(whitespace/line_length)
245 // GCC: error: call to '__fread_too_big_error' declared with attribute error: fread called with size * count bigger than buffer
246 // clang should emit a warning, but doesn't
247 fread(buf, 1, 5, stdin);
248}
249
250void test_fwrite_overflow() {
251 char buf[4];
252 // NOLINTNEXTLINE(whitespace/line_length)
253 // GCC: error: call to '__fwrite_overflow' declared with attribute error: fwrite called with overflowing size * count
254 // clang should emit a warning, but doesn't
255 fwrite(buf, 2, (size_t)-1, stdout);
256}
257
258void test_fwrite_too_big() {
259 char buf[4] = {0};
260 // NOLINTNEXTLINE(whitespace/line_length)
261 // GCC: error: call to '__fwrite_too_big_error' declared with attribute error: fwrite called with size * count bigger than buffer
262 // clang should emit a warning, but doesn't
263 fwrite(buf, 1, 5, stdout);
264}
Daniel Micay9101b002015-05-20 15:31:26 -0400265
266void test_getcwd() {
267 char buf[4];
268 // NOLINTNEXTLINE(whitespace/line_length)
269 // GCC: error: call to '__getcwd_dest_size_error' declared with attribute error: getcwd called with size bigger than destination
270 // clang should emit a warning, but doesn't
271 getcwd(buf, 5);
272}