Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <poll.h> |
| 22 | #include <stdarg.h> |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 24 | #include <stdlib.h> |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <time.h> |
| 29 | #include <unistd.h> |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 30 | |
| 31 | void test_sprintf() { |
| 32 | char buf[4]; |
| 33 | |
| 34 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 35 | // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 36 | // CLANG: error: call to unavailable function 'sprintf': format string will always overflow destination buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 37 | sprintf(buf, "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 38 | |
| 39 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 40 | // GCC: warning: call to int __builtin___sprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 41 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 42 | sprintf(buf, "%s", "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void test_snprintf() { |
| 46 | char buf[4]; |
| 47 | |
| 48 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 49 | // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 50 | // CLANG: error: call to unavailable function 'snprintf': format string will always overflow destination buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 51 | snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 52 | |
| 53 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 54 | // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 55 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 56 | snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 57 | |
| 58 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 59 | // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 60 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 61 | snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 62 | |
| 63 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 64 | // GCC: warning: call to int __builtin___snprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, ...) will always overflow destination buffer |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 65 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 66 | snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf) |
| 67 | } |
| 68 | |
| 69 | void test_memcpy() { |
| 70 | char buf[4]; |
| 71 | |
| 72 | // NOLINTNEXTLINE(whitespace/line_length) |
| 73 | // GCC: warning: call to void* __builtin___memcpy_chk(void*, const void*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 74 | // CLANG: error: 'memcpy' called with size bigger than buffer |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 75 | memcpy(buf, "foobar", sizeof("foobar") + 100); |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void test_memmove() { |
| 79 | char buf[4]; |
| 80 | |
| 81 | // NOLINTNEXTLINE(whitespace/line_length) |
| 82 | // GCC: warning: call to void* __builtin___memmove_chk(void*, const void*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 83 | // CLANG: error: 'memmove' called with size bigger than buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 84 | memmove(buf, "foobar", sizeof("foobar")); |
| 85 | } |
| 86 | |
| 87 | void test_memset() { |
| 88 | char buf[4]; |
| 89 | |
| 90 | // NOLINTNEXTLINE(whitespace/line_length) |
| 91 | // GCC: warning: call to void* __builtin___memset_chk(void*, int, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 92 | // CLANG: error: 'memset' called with size bigger than buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 93 | memset(buf, 0, 6); |
| 94 | } |
| 95 | |
| 96 | void test_strcpy() { |
| 97 | char buf[4]; |
| 98 | |
| 99 | // NOLINTNEXTLINE(whitespace/line_length) |
Yabin Cui | f3bd305 | 2015-03-04 21:43:14 -0800 | [diff] [blame] | 100 | // 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 |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 101 | // CLANG: error: 'strcpy' called with string bigger than buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 102 | strcpy(buf, "foobar"); // NOLINT(runtime/printf) |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 103 | |
| 104 | // NOLINTNEXTLINE(whitespace/line_length) |
| 105 | // 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 |
| 106 | // CLANG: error: 'strcpy' called with string bigger than buffer |
| 107 | strcpy(buf, "quux"); |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void test_stpcpy() { |
| 111 | char buf[4]; |
| 112 | |
| 113 | // NOLINTNEXTLINE(whitespace/line_length) |
| 114 | // GCC: warning: call to char* __builtin___stpcpy_chk(char*, const char*, {{(long )?}}unsigned int) will always overflow destination buffer |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 115 | // CLANG: error: 'stpcpy' called with string bigger than buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 116 | stpcpy(buf, "foobar"); |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 117 | |
| 118 | // NOLINTNEXTLINE(whitespace/line_length) |
| 119 | // GCC: warning: call to char* __builtin___stpcpy_chk(char*, const char*, {{(long )?}}unsigned int) will always overflow destination buffer |
| 120 | // CLANG: error: 'stpcpy' called with string bigger than buffer |
| 121 | stpcpy(buf, "quux"); |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void test_strncpy() { |
| 125 | char buf[4]; |
| 126 | |
| 127 | // NOLINTNEXTLINE(whitespace/line_length) |
| 128 | // GCC: warning: call to char* __builtin___strncpy_chk(char*, const char*, {{(long )?}}unsigned int, {{(long )?}}unsigned int) will always overflow destination buffer |
| 129 | // clang should emit a warning, but doesn't |
| 130 | strncpy(buf, "foobar", sizeof("foobar")); |
| 131 | } |
| 132 | |
| 133 | void test_strcat() { |
| 134 | char buf[4] = ""; |
| 135 | |
| 136 | // NOLINTNEXTLINE(whitespace/line_length) |
Yabin Cui | f3bd305 | 2015-03-04 21:43:14 -0800 | [diff] [blame] | 137 | // 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 Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 138 | // clang should emit a warning, but doesn't |
| 139 | strcat(buf, "foobar"); // NOLINT(runtime/printf) |
| 140 | } |
| 141 | |
| 142 | void test_strncat() { |
| 143 | char buf[4] = ""; |
| 144 | |
| 145 | // NOLINTNEXTLINE(whitespace/line_length) |
Yabin Cui | f3bd305 | 2015-03-04 21:43:14 -0800 | [diff] [blame] | 146 | // 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 Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 147 | // gcc output warning with __builtin___strcat_chk for __builtin___strncat_chk. |
| 148 | // clang should emit a warning, but doesn't |
| 149 | strncat(buf, "foobar", sizeof("foobar")); |
| 150 | } |
| 151 | |
| 152 | void test_vsprintf(const char* fmt, ...) { |
| 153 | va_list va; |
| 154 | char buf[4]; |
| 155 | va_start(va, fmt); |
| 156 | |
| 157 | // NOLINTNEXTLINE(whitespace/line_length) |
Yabin Cui | d964759 | 2015-03-05 00:39:09 -0800 | [diff] [blame] | 158 | // 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 Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 159 | // clang should emit a warning, but doesn't |
| 160 | vsprintf(buf, "foobar", va); |
| 161 | va_end(va); |
| 162 | } |
| 163 | |
| 164 | void test_vsnprintf(const char* fmt, ...) { |
| 165 | va_list va; |
| 166 | char buf[4]; |
| 167 | va_start(va, fmt); |
| 168 | |
| 169 | // NOLINTNEXTLINE(whitespace/line_length) |
Yabin Cui | d964759 | 2015-03-05 00:39:09 -0800 | [diff] [blame] | 170 | // 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 Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 171 | // clang should emit a warning, but doesn't |
| 172 | vsnprintf(buf, 5, "foobar", va); // NOLINT(runtime/printf) |
| 173 | |
| 174 | va_end(va); |
| 175 | } |
| 176 | |
| 177 | void test_fgets() { |
| 178 | char buf[4]; |
| 179 | |
| 180 | // NOLINTNEXTLINE(whitespace/line_length) |
| 181 | // GCC: error: call to '__fgets_too_small_error' declared with attribute error: fgets called with size less than zero |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 182 | // CLANG: error: in call to 'fgets', size should not be negative |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 183 | fgets(buf, -1, stdin); |
| 184 | |
| 185 | // NOLINTNEXTLINE(whitespace/line_length) |
| 186 | // GCC: error: call to '__fgets_too_big_error' declared with attribute error: fgets called with size bigger than buffer |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 187 | // CLANG: error: in call to 'fgets', size is larger than the destination buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 188 | fgets(buf, 6, stdin); |
| 189 | } |
| 190 | |
| 191 | void test_recvfrom() { |
| 192 | char buf[4]; |
| 193 | sockaddr_in addr; |
| 194 | |
| 195 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 196 | // GCC: error: call to '__recvfrom_error' declared with attribute error: 'recvfrom' called with size bigger than buffer |
| 197 | // CLANG: error: 'recvfrom' called with size bigger than buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 198 | recvfrom(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), NULL); |
| 199 | } |
| 200 | |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 201 | void test_recv() { |
| 202 | char buf[4] = {0}; |
| 203 | |
| 204 | // NOLINTNEXTLINE(whitespace/line_length) |
| 205 | // GCC: error: call to '__recvfrom_error' declared with attribute error: 'recvfrom' called with size bigger than buffer |
| 206 | // CLANG: error: 'recv' called with size bigger than buffer |
| 207 | recv(0, buf, 6, 0); |
| 208 | } |
| 209 | |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 210 | void test_umask() { |
| 211 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | 52dde5f | 2017-07-31 21:16:05 -0700 | [diff] [blame] | 212 | // GCC: error: call to '__umask_invalid_mode' declared with attribute error: 'umask' called with invalid mode |
| 213 | // CLANG: error: 'umask' called with invalid mode |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 214 | umask(01777); |
| 215 | } |
| 216 | |
| 217 | void test_read() { |
| 218 | char buf[4]; |
| 219 | // NOLINTNEXTLINE(whitespace/line_length) |
| 220 | // GCC: error: call to '__read_dest_size_error' declared with attribute error: read called with size bigger than destination |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 221 | // CLANG: error: in call to 'read', 'count' bytes overflows the given object |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 222 | read(0, buf, 6); |
| 223 | } |
| 224 | |
| 225 | void test_open() { |
| 226 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 227 | // GCC: error: call to '__creat_missing_mode' declared with attribute error: called with O_CREAT or O_TMPFILE, but missing mode |
| 228 | // CLANG: error: 'open' called with O_CREAT or O_TMPFILE, but missing mode |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 229 | open("/dev/null", O_CREAT); |
| 230 | |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 231 | // GCC: error: call to '__creat_missing_mode' declared with attribute error: called with O_CREAT or O_TMPFILE, but missing mode |
| 232 | // CLANG: error: 'open' called with O_CREAT or O_TMPFILE, but missing mode |
| 233 | open("/dev/null", O_TMPFILE); |
| 234 | |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 235 | // NOLINTNEXTLINE(whitespace/line_length) |
| 236 | // GCC: error: call to '__creat_too_many_args' declared with attribute error: too many arguments |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 237 | // CLANG: error: call to unavailable function 'open': too many arguments |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 238 | open("/dev/null", O_CREAT, 0, 0); |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 239 | |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 240 | // GCC: error: call to '__creat_too_many_args' declared with attribute error: too many arguments |
| 241 | // CLANG: error: call to unavailable function 'open': too many arguments |
| 242 | open("/dev/null", O_TMPFILE, 0, 0); |
| 243 | |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 244 | // CLANG: warning: 'open' has superfluous mode bits; missing O_CREAT? |
| 245 | open("/dev/null", O_RDONLY, 0644); |
| 246 | |
| 247 | // CLANG: warning: 'open' has superfluous mode bits; missing O_CREAT? |
| 248 | open("/dev/null", O_DIRECTORY, 0644); |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void test_poll() { |
| 252 | pollfd fds[1]; |
| 253 | // NOLINTNEXTLINE(whitespace/line_length) |
| 254 | // GCC: error: call to '__poll_too_small_error' declared with attribute error: poll: pollfd array smaller than fd count |
George Burgess IV | 52dde5f | 2017-07-31 21:16:05 -0700 | [diff] [blame] | 255 | // CLANG: error: in call to 'poll', fd_count is larger than the given buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 256 | poll(fds, 2, 0); |
| 257 | } |
| 258 | |
| 259 | void test_ppoll() { |
| 260 | pollfd fds[1]; |
| 261 | timespec timeout; |
| 262 | // NOLINTNEXTLINE(whitespace/line_length) |
| 263 | // GCC: error: call to '__ppoll_too_small_error' declared with attribute error: ppoll: pollfd array smaller than fd count |
George Burgess IV | 52dde5f | 2017-07-31 21:16:05 -0700 | [diff] [blame] | 264 | // CLANG: error: in call to 'ppoll', fd_count is larger than the given buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 265 | ppoll(fds, 2, &timeout, NULL); |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 266 | } |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 267 | |
| 268 | void test_fread_overflow() { |
| 269 | char buf[4]; |
| 270 | // NOLINTNEXTLINE(whitespace/line_length) |
| 271 | // GCC: error: call to '__fread_overflow' declared with attribute error: fread called with overflowing size * count |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 272 | // CLANG: error: in call to 'fread', size * count overflows |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 273 | fread(buf, 2, (size_t)-1, stdin); |
| 274 | } |
| 275 | |
| 276 | void test_fread_too_big() { |
| 277 | char buf[4]; |
| 278 | // NOLINTNEXTLINE(whitespace/line_length) |
| 279 | // GCC: error: call to '__fread_too_big_error' declared with attribute error: fread called with size * count bigger than buffer |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 280 | // NOLINTNEXTLINE(whitespace/line_length) |
| 281 | // CLANG: error: in call to 'fread', size * count is too large for the given buffer |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 282 | fread(buf, 1, 5, stdin); |
| 283 | } |
| 284 | |
| 285 | void test_fwrite_overflow() { |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 286 | char buf[4] = {0}; |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 287 | // NOLINTNEXTLINE(whitespace/line_length) |
| 288 | // GCC: error: call to '__fwrite_overflow' declared with attribute error: fwrite called with overflowing size * count |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 289 | // CLANG: error: in call to 'fwrite', size * count overflows |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 290 | fwrite(buf, 2, (size_t)-1, stdout); |
| 291 | } |
| 292 | |
| 293 | void test_fwrite_too_big() { |
| 294 | char buf[4] = {0}; |
| 295 | // NOLINTNEXTLINE(whitespace/line_length) |
| 296 | // GCC: error: call to '__fwrite_too_big_error' declared with attribute error: fwrite called with size * count bigger than buffer |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 297 | // NOLINTNEXTLINE(whitespace/line_length) |
| 298 | // CLANG: error: in call to 'fwrite', size * count is too large for the given buffer |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 299 | fwrite(buf, 1, 5, stdout); |
| 300 | } |
Daniel Micay | 9101b00 | 2015-05-20 15:31:26 -0400 | [diff] [blame] | 301 | |
| 302 | void test_getcwd() { |
| 303 | char buf[4]; |
| 304 | // NOLINTNEXTLINE(whitespace/line_length) |
| 305 | // GCC: error: call to '__getcwd_dest_size_error' declared with attribute error: getcwd called with size bigger than destination |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 306 | // CLANG: error: in call to 'getcwd', 'size' bytes overflows the given object |
Daniel Micay | 9101b00 | 2015-05-20 15:31:26 -0400 | [diff] [blame] | 307 | getcwd(buf, 5); |
| 308 | } |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 309 | |
| 310 | void test_pwrite64_size() { |
| 311 | char buf[4] = {0}; |
| 312 | // NOLINTNEXTLINE(whitespace/line_length) |
| 313 | // GCC: error: call to '__pwrite64_dest_size_error' declared with attribute error: pwrite64 called with size bigger than destination |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 314 | // CLANG: error: in call to 'pwrite64', 'count' bytes overflows the given object |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 315 | pwrite64(STDOUT_FILENO, buf, 5, 0); |
| 316 | } |
| 317 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 318 | void test_pwrite64_too_big_malloc() { |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 319 | void *buf = calloc(atoi("5"), 1); |
| 320 | // NOLINTNEXTLINE(whitespace/line_length) |
| 321 | // GCC: error: call to '__pwrite64_count_toobig_error' declared with attribute error: pwrite64 called with count > SSIZE_MAX |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 322 | // clang should emit a warning, but probably never will. |
| 323 | pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0); |
| 324 | } |
| 325 | |
| 326 | void test_pwrite64_too_big() { |
| 327 | char buf[4] = {0}; |
| 328 | // NOLINTNEXTLINE(whitespace/line_length) |
| 329 | // GCC: error: call to '__pwrite64_count_toobig_error' declared with attribute error: pwrite64 called with count > SSIZE_MAX |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 330 | // CLANG: error: in call to 'pwrite64', 'count' must be <= SSIZE_MAX |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 331 | pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0); |
| 332 | } |
| 333 | |
| 334 | void test_write_size() { |
| 335 | char buf[4] = {0}; |
| 336 | // NOLINTNEXTLINE(whitespace/line_length) |
| 337 | // GCC: error: call to '__write_dest_size_error' declared with attribute error: write called with size bigger than destination |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 338 | // CLANG: error: in call to 'write', 'count' bytes overflows the given object |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 339 | write(STDOUT_FILENO, buf, 5); |
| 340 | } |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 341 | |
| 342 | void test_memset_args_flipped() { |
| 343 | char from[4] = {0}; |
| 344 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 345 | // CLANG: 'memset' will set 0 bytes; maybe the arguments got flipped? |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 346 | memset(from, sizeof(from), 0); |
| 347 | } |
Daniel Micay | 95b59c5 | 2017-02-13 17:27:59 -0800 | [diff] [blame] | 348 | |
| 349 | void test_sendto() { |
| 350 | char buf[4] = {0}; |
| 351 | sockaddr_in addr; |
| 352 | |
| 353 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 354 | // GCC: error: call to '__sendto_error' declared with attribute error: 'sendto' called with size bigger than buffer |
| 355 | // CLANG: error: 'sendto' called with size bigger than buffer |
Daniel Micay | 95b59c5 | 2017-02-13 17:27:59 -0800 | [diff] [blame] | 356 | sendto(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr_in)); |
| 357 | } |
| 358 | |
| 359 | void test_send() { |
| 360 | char buf[4] = {0}; |
| 361 | |
| 362 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 363 | // GCC: error: call to '__sendto_error' declared with attribute error: 'sendto' called with size bigger than buffer |
| 364 | // CLANG: error: 'send' called with size bigger than buffer |
Daniel Micay | 95b59c5 | 2017-02-13 17:27:59 -0800 | [diff] [blame] | 365 | send(0, buf, 6, 0); |
| 366 | } |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 367 | |
| 368 | void test_realpath() { |
| 369 | char buf[4] = {0}; |
| 370 | // NOLINTNEXTLINE(whitespace/line_length) |
| 371 | // GCC: error: call to '__realpath_size_error' declared with attribute error: 'realpath' output parameter must be NULL or a pointer to a buffer with >= PATH_MAX bytes |
| 372 | // NOLINTNEXTLINE(whitespace/line_length) |
| 373 | // CLANG: error: 'realpath' output parameter must be NULL or a pointer to a buffer with >= PATH_MAX bytes |
| 374 | realpath(".", buf); |
| 375 | |
| 376 | // This is fine. |
| 377 | realpath(".", NULL); |
| 378 | |
George Burgess IV | 95bd488 | 2017-08-14 14:48:55 -0700 | [diff] [blame] | 379 | char bigbuf[PATH_MAX]; |
| 380 | // CLANG: error: 'realpath': NULL path is never correct; flipped arguments? |
| 381 | realpath(NULL, bigbuf); |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 382 | } |