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> |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <time.h> |
| 28 | #include <unistd.h> |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 29 | |
| 30 | void test_sprintf() { |
| 31 | char buf[4]; |
| 32 | |
| 33 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 34 | // 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] | 35 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 36 | sprintf(buf, "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 37 | |
| 38 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 39 | // 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] | 40 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 41 | sprintf(buf, "%s", "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void test_snprintf() { |
| 45 | char buf[4]; |
| 46 | |
| 47 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 48 | // 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] | 49 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 50 | snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 51 | |
| 52 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 53 | // 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] | 54 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 55 | snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 56 | |
| 57 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 58 | // 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] | 59 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 60 | snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 61 | |
| 62 | // NOLINTNEXTLINE(whitespace/line_length) |
Elliott Hughes | b4b15c6 | 2014-10-08 13:21:29 -0700 | [diff] [blame] | 63 | // 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] | 64 | // clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 65 | snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf) |
| 66 | } |
| 67 | |
| 68 | void 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 | |
| 77 | void 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 | |
| 86 | void 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 | |
| 95 | void test_strcpy() { |
| 96 | char buf[4]; |
| 97 | |
| 98 | // NOLINTNEXTLINE(whitespace/line_length) |
Yabin Cui | f3bd305 | 2015-03-04 21:43:14 -0800 | [diff] [blame] | 99 | // 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 Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 100 | // clang should emit a warning, but doesn't |
| 101 | strcpy(buf, "foobar"); // NOLINT(runtime/printf) |
| 102 | } |
| 103 | |
| 104 | void 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 | |
| 113 | void 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 | |
| 122 | void test_strcat() { |
| 123 | char buf[4] = ""; |
| 124 | |
| 125 | // NOLINTNEXTLINE(whitespace/line_length) |
Yabin Cui | f3bd305 | 2015-03-04 21:43:14 -0800 | [diff] [blame] | 126 | // 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] | 127 | // clang should emit a warning, but doesn't |
| 128 | strcat(buf, "foobar"); // NOLINT(runtime/printf) |
| 129 | } |
| 130 | |
| 131 | void test_strncat() { |
| 132 | char buf[4] = ""; |
| 133 | |
| 134 | // NOLINTNEXTLINE(whitespace/line_length) |
Yabin Cui | f3bd305 | 2015-03-04 21:43:14 -0800 | [diff] [blame] | 135 | // 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] | 136 | // 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 | |
| 141 | void 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 Cui | 94545eb | 2015-03-04 22:35:13 -0800 | [diff] [blame^] | 147 | // GCC: warning: call to int __builtin___vsprintf_chk(char*, int, {{(long )?}}unsigned int, const char*, {{(__va_list)|(void\*)|(char\*)}}) will always overflow destination buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 148 | // clang should emit a warning, but doesn't |
| 149 | vsprintf(buf, "foobar", va); |
| 150 | va_end(va); |
| 151 | } |
| 152 | |
| 153 | void 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 Cui | 94545eb | 2015-03-04 22:35:13 -0800 | [diff] [blame^] | 159 | // GCC: warning: call to int __builtin___vsnprintf_chk(char*, {{(long )?}}unsigned int, int, {{(long )?}}unsigned int, const char*, {{(__va_list)|(void\*)|(char\*)}}) will always overflow destination buffer |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 160 | // clang should emit a warning, but doesn't |
| 161 | vsnprintf(buf, 5, "foobar", va); // NOLINT(runtime/printf) |
| 162 | |
| 163 | va_end(va); |
| 164 | } |
| 165 | |
| 166 | void 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 | |
| 180 | void 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 | |
| 190 | void 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 | |
| 197 | void 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 | |
| 205 | void 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 | |
| 217 | void 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 | |
| 225 | void 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 Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 232 | } |