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 | |
George Burgess IV | c7bd90f | 2017-08-23 17:32:48 -0700 | [diff] [blame] | 17 | /* |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 18 | * Silence all notes about enable_if-related 'candidates'; they're nice to know |
| 19 | * about for users, but this test doesn't care. |
George Burgess IV | c7bd90f | 2017-08-23 17:32:48 -0700 | [diff] [blame] | 20 | */ |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 21 | // expected-note@* 0+{{candidate function}} |
| 22 | |
| 23 | /* Similarly, ignore all "from 'diagnose_if'"s. */ |
| 24 | // expected-note@* 0+{{from 'diagnose_if'}} |
| 25 | |
George Burgess IV | c7bd90f | 2017-08-23 17:32:48 -0700 | [diff] [blame] | 26 | |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 27 | #undef _FORTIFY_SOURCE |
| 28 | #define _FORTIFY_SOURCE 2 |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <netinet/in.h> |
| 31 | #include <poll.h> |
| 32 | #include <stdarg.h> |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 33 | #include <stdio.h> |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 34 | #include <stdlib.h> |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 35 | #include <string.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <time.h> |
| 39 | #include <unistd.h> |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 40 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 41 | #if !defined(__BIONIC__) |
| 42 | # error "This only works with Bionic." |
| 43 | #endif |
| 44 | |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 45 | void test_sprintf() { |
| 46 | char buf[4]; |
| 47 | |
| 48 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 49 | // expected-error@+1{{call to unavailable function 'sprintf': format string will always overflow destination buffer}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 50 | sprintf(buf, "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | 0d1a8a5 | 2018-07-24 19:36:51 +0000 | [diff] [blame] | 52 | // TODO: clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 53 | sprintf(buf, "%s", "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void test_snprintf() { |
| 57 | char buf[4]; |
| 58 | |
| 59 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 60 | // expected-error@+1{{call to unavailable function 'snprintf': format string will always overflow destination buffer}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 61 | snprintf(buf, 5, "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 62 | |
Elliott Hughes | 0d1a8a5 | 2018-07-24 19:36:51 +0000 | [diff] [blame] | 63 | // TODO: clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 64 | snprintf(buf, 5, "%s", "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | 0d1a8a5 | 2018-07-24 19:36:51 +0000 | [diff] [blame] | 66 | // TODO: clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 67 | snprintf(buf, 5, " %s ", "foobar"); // NOLINT(runtime/printf) |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 68 | |
Elliott Hughes | 0d1a8a5 | 2018-07-24 19:36:51 +0000 | [diff] [blame] | 69 | // TODO: clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 70 | snprintf(buf, 5, "%d", 100000); // NOLINT(runtime/printf) |
| 71 | } |
| 72 | |
| 73 | void test_memcpy() { |
| 74 | char buf[4]; |
| 75 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 76 | // expected-error@+1{{'memcpy' called with size bigger than buffer}} |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 77 | memcpy(buf, "foobar", sizeof("foobar") + 100); |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void test_memmove() { |
| 81 | char buf[4]; |
| 82 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 83 | // expected-error@+1{{'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 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 90 | // expected-error@+1{{'memset' called with size bigger than buffer}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 91 | memset(buf, 0, 6); |
| 92 | } |
| 93 | |
| 94 | void test_strcpy() { |
| 95 | char buf[4]; |
| 96 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 97 | // expected-error@+1{{'strcpy' called with string bigger than buffer}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 98 | strcpy(buf, "foobar"); // NOLINT(runtime/printf) |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 99 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 100 | // expected-error@+1{{'strcpy' called with string bigger than buffer}} |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 101 | strcpy(buf, "quux"); |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void test_stpcpy() { |
| 105 | char buf[4]; |
| 106 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 107 | // expected-error@+1{{'stpcpy' called with string bigger than buffer}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 108 | stpcpy(buf, "foobar"); |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 109 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 110 | // expected-error@+1{{'stpcpy' called with string bigger than buffer}} |
George Burgess IV | b630046 | 2017-07-31 21:29:42 -0700 | [diff] [blame] | 111 | stpcpy(buf, "quux"); |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void test_strncpy() { |
| 115 | char buf[4]; |
| 116 | |
Elliott Hughes | 0d1a8a5 | 2018-07-24 19:36:51 +0000 | [diff] [blame] | 117 | // TODO: clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 118 | strncpy(buf, "foobar", sizeof("foobar")); |
| 119 | } |
| 120 | |
| 121 | void test_strcat() { |
| 122 | char buf[4] = ""; |
| 123 | |
Elliott Hughes | 0d1a8a5 | 2018-07-24 19:36:51 +0000 | [diff] [blame] | 124 | // TODO: clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 125 | strcat(buf, "foobar"); // NOLINT(runtime/printf) |
| 126 | } |
| 127 | |
| 128 | void test_strncat() { |
| 129 | char buf[4] = ""; |
| 130 | |
Elliott Hughes | 0d1a8a5 | 2018-07-24 19:36:51 +0000 | [diff] [blame] | 131 | // TODO: clang should emit a warning, but doesn't |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 132 | strncat(buf, "foobar", sizeof("foobar")); |
| 133 | } |
| 134 | |
| 135 | void test_vsprintf(const char* fmt, ...) { |
| 136 | va_list va; |
| 137 | char buf[4]; |
| 138 | va_start(va, fmt); |
| 139 | |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 140 | // clang should emit a warning, but doesn't |
| 141 | vsprintf(buf, "foobar", va); |
| 142 | va_end(va); |
| 143 | } |
| 144 | |
| 145 | void test_vsnprintf(const char* fmt, ...) { |
| 146 | va_list va; |
| 147 | char buf[4]; |
| 148 | va_start(va, fmt); |
| 149 | |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 150 | // clang should emit a warning, but doesn't |
| 151 | vsnprintf(buf, 5, "foobar", va); // NOLINT(runtime/printf) |
| 152 | |
| 153 | va_end(va); |
| 154 | } |
| 155 | |
| 156 | void test_fgets() { |
| 157 | char buf[4]; |
| 158 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 159 | // expected-error@+1{{in call to 'fgets', size should not be negative}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 160 | fgets(buf, -1, stdin); |
| 161 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 162 | // expected-error@+1{{in call to 'fgets', size is larger than the destination buffer}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 163 | fgets(buf, 6, stdin); |
| 164 | } |
| 165 | |
| 166 | void test_recvfrom() { |
| 167 | char buf[4]; |
| 168 | sockaddr_in addr; |
| 169 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 170 | // expected-error@+1{{'recvfrom' called with size bigger than buffer}} |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 171 | recvfrom(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), nullptr); |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 172 | } |
| 173 | |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 174 | void test_recv() { |
| 175 | char buf[4] = {0}; |
| 176 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 177 | // expected-error@+1{{'recv' called with size bigger than buffer}} |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 178 | recv(0, buf, 6, 0); |
| 179 | } |
| 180 | |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 181 | void test_umask() { |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 182 | // expected-error@+1{{'umask' called with invalid mode}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 183 | umask(01777); |
| 184 | } |
| 185 | |
| 186 | void test_read() { |
| 187 | char buf[4]; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 188 | // expected-error@+1{{in call to 'read', 'count' bytes overflows the given object}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 189 | read(0, buf, 6); |
| 190 | } |
| 191 | |
| 192 | void test_open() { |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 193 | // expected-error@+1{{'open' called with O_CREAT or O_TMPFILE, but missing mode}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 194 | open("/dev/null", O_CREAT); |
| 195 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 196 | // expected-error@+1{{'open' called with O_CREAT or O_TMPFILE, but missing mode}} |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 197 | open("/dev/null", O_TMPFILE); |
| 198 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 199 | // expected-error@+1{{call to unavailable function 'open': too many arguments}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 200 | open("/dev/null", O_CREAT, 0, 0); |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 201 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 202 | // expected-error@+1{{call to unavailable function 'open': too many arguments}} |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 203 | open("/dev/null", O_TMPFILE, 0, 0); |
| 204 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 205 | // expected-warning@+1{{'open' has superfluous mode bits; missing O_CREAT?}} |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 206 | open("/dev/null", O_RDONLY, 0644); |
| 207 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 208 | // expected-warning@+1{{'open' has superfluous mode bits; missing O_CREAT?}} |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 209 | open("/dev/null", O_DIRECTORY, 0644); |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void test_poll() { |
| 213 | pollfd fds[1]; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 214 | // expected-error@+1{{in call to 'poll', fd_count is larger than the given buffer}} |
Yabin Cui | 20f2268 | 2015-03-03 20:27:58 -0800 | [diff] [blame] | 215 | poll(fds, 2, 0); |
| 216 | } |
| 217 | |
| 218 | void test_ppoll() { |
| 219 | pollfd fds[1]; |
| 220 | timespec timeout; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 221 | // expected-error@+1{{in call to 'ppoll', fd_count is larger than the given buffer}} |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 222 | ppoll(fds, 2, &timeout, nullptr); |
| 223 | } |
| 224 | |
| 225 | void test_ppoll64() { |
| 226 | pollfd fds[1]; |
| 227 | timespec timeout; |
| 228 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 229 | // expected-error@+1{{in call to 'ppoll64', fd_count is larger than the given buffer}} |
Elliott Hughes | b83bf14 | 2018-03-22 11:01:25 -0700 | [diff] [blame] | 230 | ppoll64(fds, 2, &timeout, nullptr); |
Dan Albert | 2fbb1b6 | 2014-10-08 11:21:32 -0700 | [diff] [blame] | 231 | } |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 232 | |
| 233 | void test_fread_overflow() { |
| 234 | char buf[4]; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 235 | // expected-error@+1{{in call to 'fread', size * count overflows}} |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 236 | fread(buf, 2, (size_t)-1, stdin); |
| 237 | } |
| 238 | |
| 239 | void test_fread_too_big() { |
| 240 | char buf[4]; |
| 241 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 242 | // expected-error@+1{{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] | 243 | fread(buf, 1, 5, stdin); |
| 244 | } |
| 245 | |
| 246 | void test_fwrite_overflow() { |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 247 | char buf[4] = {0}; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 248 | // expected-error@+1{{in call to 'fwrite', size * count overflows}} |
Daniel Micay | fed2659 | 2015-07-18 13:55:51 -0400 | [diff] [blame] | 249 | fwrite(buf, 2, (size_t)-1, stdout); |
| 250 | } |
| 251 | |
| 252 | void test_fwrite_too_big() { |
| 253 | char buf[4] = {0}; |
| 254 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 255 | // expected-error@+1{{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] | 256 | fwrite(buf, 1, 5, stdout); |
| 257 | } |
Daniel Micay | 9101b00 | 2015-05-20 15:31:26 -0400 | [diff] [blame] | 258 | |
| 259 | void test_getcwd() { |
| 260 | char buf[4]; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 261 | // expected-error@+1{{in call to 'getcwd', 'size' bytes overflows the given object}} |
Daniel Micay | 9101b00 | 2015-05-20 15:31:26 -0400 | [diff] [blame] | 262 | getcwd(buf, 5); |
| 263 | } |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 264 | |
| 265 | void test_pwrite64_size() { |
| 266 | char buf[4] = {0}; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 267 | // expected-error@+1{{in call to 'pwrite64', 'count' bytes overflows the given object}} |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 268 | pwrite64(STDOUT_FILENO, buf, 5, 0); |
| 269 | } |
| 270 | |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 271 | void test_pwrite64_too_big_malloc() { |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 272 | void *buf = calloc(atoi("5"), 1); |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 273 | // expected-error@+1{{in call to 'pwrite64', 'count' must be <= SSIZE_MAX}} |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 274 | pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0); |
| 275 | } |
| 276 | |
| 277 | void test_pwrite64_too_big() { |
| 278 | char buf[4] = {0}; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 279 | // expected-error@+1{{in call to 'pwrite64', 'count' must be <= SSIZE_MAX}} |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 280 | pwrite64(STDOUT_FILENO, buf, SIZE_MAX, 0); |
| 281 | } |
| 282 | |
| 283 | void test_write_size() { |
| 284 | char buf[4] = {0}; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 285 | // expected-error@+1{{in call to 'write', 'count' bytes overflows the given object}} |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 286 | write(STDOUT_FILENO, buf, 5); |
| 287 | } |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 288 | |
| 289 | void test_memset_args_flipped() { |
| 290 | char from[4] = {0}; |
| 291 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 292 | // expected-warning@+1{{'memset' will set 0 bytes; maybe the arguments got flipped?}} |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 293 | memset(from, sizeof(from), 0); |
| 294 | } |
Daniel Micay | 95b59c5 | 2017-02-13 17:27:59 -0800 | [diff] [blame] | 295 | |
| 296 | void test_sendto() { |
| 297 | char buf[4] = {0}; |
| 298 | sockaddr_in addr; |
| 299 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 300 | // expected-error@+1{{'sendto' called with size bigger than buffer}} |
Daniel Micay | 95b59c5 | 2017-02-13 17:27:59 -0800 | [diff] [blame] | 301 | sendto(0, buf, 6, 0, reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr_in)); |
| 302 | } |
| 303 | |
| 304 | void test_send() { |
| 305 | char buf[4] = {0}; |
| 306 | |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 307 | // expected-error@+1{{'send' called with size bigger than buffer}} |
Daniel Micay | 95b59c5 | 2017-02-13 17:27:59 -0800 | [diff] [blame] | 308 | send(0, buf, 6, 0); |
| 309 | } |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 310 | |
| 311 | void test_realpath() { |
| 312 | char buf[4] = {0}; |
| 313 | // NOLINTNEXTLINE(whitespace/line_length) |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 314 | // expected-error@+1{{'realpath' output parameter must be NULL or a pointer to a buffer with >= PATH_MAX bytes}} |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 315 | realpath(".", buf); |
| 316 | |
| 317 | // This is fine. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 318 | realpath(".", nullptr); |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 319 | |
George Burgess IV | 95bd488 | 2017-08-14 14:48:55 -0700 | [diff] [blame] | 320 | char bigbuf[PATH_MAX]; |
George Burgess IV | db48e0c | 2019-05-02 16:23:31 -0700 | [diff] [blame] | 321 | // expected-error@+1{{'realpath': NULL path is never correct; flipped arguments?}} |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 322 | realpath(nullptr, bigbuf); |
George Burgess IV | 54f5d83 | 2017-07-31 21:21:10 -0700 | [diff] [blame] | 323 | } |