Jordy Rose | 467f7c8 | 2011-06-14 01:40:43 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s |
| 2 | // RUN: %clang_cc1 -analyze -DUSE_BUILTINS -analyzer-checker=core,unix.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s |
| 3 | // RUN: %clang_cc1 -analyze -DVARIANT -analyzer-checker=core,unix.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s |
| 4 | // RUN: %clang_cc1 -analyze -DUSE_BUILTINS -DVARIANT -analyzer-checker=core,unix.experimental.CString,deadcode.experimental.UnreachableCode -analyzer-store=region -Wno-null-dereference -verify %s |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 5 | |
| 6 | //===----------------------------------------------------------------------=== |
| 7 | // Declarations |
| 8 | //===----------------------------------------------------------------------=== |
| 9 | |
| 10 | // Some functions are so similar to each other that they follow the same code |
| 11 | // path, such as memcpy and __memcpy_chk, or memcmp and bcmp. If VARIANT is |
| 12 | // defined, make sure to use the variants instead to make sure they are still |
| 13 | // checked by the analyzer. |
| 14 | |
| 15 | // Some functions are implemented as builtins. These should be #defined as |
| 16 | // BUILTIN(f), which will prepend "__builtin_" if USE_BUILTINS is defined. |
| 17 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 18 | // Functions that have variants and are also available as builtins should be |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 19 | // declared carefully! See memcpy() for an example. |
| 20 | |
| 21 | #ifdef USE_BUILTINS |
| 22 | # define BUILTIN(f) __builtin_ ## f |
| 23 | #else /* USE_BUILTINS */ |
| 24 | # define BUILTIN(f) f |
| 25 | #endif /* USE_BUILTINS */ |
| 26 | |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 27 | #define NULL 0 |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 28 | typedef typeof(sizeof(int)) size_t; |
| 29 | |
| 30 | //===----------------------------------------------------------------------=== |
| 31 | // strlen() |
| 32 | //===----------------------------------------------------------------------=== |
| 33 | |
| 34 | #define strlen BUILTIN(strlen) |
| 35 | size_t strlen(const char *s); |
| 36 | |
| 37 | void strlen_constant0() { |
| 38 | if (strlen("123") != 3) |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 39 | (void)*(char*)0; // no-warning |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void strlen_constant1() { |
| 43 | const char *a = "123"; |
| 44 | if (strlen(a) != 3) |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 45 | (void)*(char*)0; // no-warning |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void strlen_constant2(char x) { |
| 49 | char a[] = "123"; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 50 | if (strlen(a) != 3) |
| 51 | (void)*(char*)0; // no-warning |
Jordy Rose | 19c5dd1 | 2010-07-27 01:37:31 +0000 | [diff] [blame] | 52 | a[0] = x; |
| 53 | if (strlen(a) != 3) |
| 54 | (void)*(char*)0; // expected-warning{{null}} |
| 55 | } |
| 56 | |
| 57 | size_t strlen_null() { |
| 58 | return strlen(0); // expected-warning{{Null pointer argument in call to byte string function}} |
| 59 | } |
| 60 | |
| 61 | size_t strlen_fn() { |
| 62 | return strlen((char*)&strlen_fn); // expected-warning{{Argument to byte string function is the address of the function 'strlen_fn', which is not a null-terminated string}} |
| 63 | } |
| 64 | |
| 65 | size_t strlen_nonloc() { |
| 66 | label: |
| 67 | return strlen((char*)&&label); // expected-warning{{Argument to byte string function is the address of the label 'label', which is not a null-terminated string}} |
| 68 | } |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 69 | |
| 70 | void strlen_subregion() { |
Carl Norum | e224ba7 | 2011-03-07 22:57:45 +0000 | [diff] [blame] | 71 | struct two_strings { char a[2], b[2]; }; |
Jordy Rose | a526154 | 2010-08-14 21:02:52 +0000 | [diff] [blame] | 72 | extern void use_two_strings(struct two_strings *); |
| 73 | |
| 74 | struct two_strings z; |
| 75 | use_two_strings(&z); |
| 76 | |
| 77 | size_t a = strlen(z.a); |
| 78 | z.b[0] = 5; |
| 79 | size_t b = strlen(z.a); |
| 80 | if (a == 0 && b != 0) |
| 81 | (void)*(char*)0; // expected-warning{{never executed}} |
| 82 | |
| 83 | use_two_strings(&z); |
| 84 | |
| 85 | size_t c = strlen(z.a); |
| 86 | if (a == 0 && c != 0) |
| 87 | (void)*(char*)0; // expected-warning{{null}} |
| 88 | } |
| 89 | |
| 90 | extern void use_string(char *); |
| 91 | void strlen_argument(char *x) { |
| 92 | size_t a = strlen(x); |
| 93 | size_t b = strlen(x); |
| 94 | if (a == 0 && b != 0) |
| 95 | (void)*(char*)0; // expected-warning{{never executed}} |
| 96 | |
| 97 | use_string(x); |
| 98 | |
| 99 | size_t c = strlen(x); |
| 100 | if (a == 0 && c != 0) |
| 101 | (void)*(char*)0; // expected-warning{{null}} |
| 102 | } |
| 103 | |
| 104 | extern char global_str[]; |
| 105 | void strlen_global() { |
| 106 | size_t a = strlen(global_str); |
| 107 | size_t b = strlen(global_str); |
| 108 | if (a == 0 && b != 0) |
| 109 | (void)*(char*)0; // expected-warning{{never executed}} |
| 110 | |
| 111 | // Call a function with unknown effects, which should invalidate globals. |
| 112 | use_string(0); |
| 113 | |
| 114 | size_t c = strlen(global_str); |
| 115 | if (a == 0 && c != 0) |
| 116 | (void)*(char*)0; // expected-warning{{null}} |
| 117 | } |
| 118 | |
| 119 | void strlen_indirect(char *x) { |
| 120 | size_t a = strlen(x); |
| 121 | char *p = x; |
| 122 | char **p2 = &p; |
| 123 | size_t b = strlen(x); |
| 124 | if (a == 0 && b != 0) |
| 125 | (void)*(char*)0; // expected-warning{{never executed}} |
| 126 | |
| 127 | extern void use_string_ptr(char*const*); |
| 128 | use_string_ptr(p2); |
| 129 | |
| 130 | size_t c = strlen(x); |
| 131 | if (a == 0 && c != 0) |
| 132 | (void)*(char*)0; // expected-warning{{null}} |
| 133 | } |
| 134 | |
| 135 | void strlen_liveness(const char *x) { |
| 136 | if (strlen(x) < 5) |
| 137 | return; |
| 138 | if (strlen(x) < 5) |
| 139 | (void)*(char*)0; // no-warning |
| 140 | } |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 141 | |
| 142 | //===----------------------------------------------------------------------=== |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 143 | // strnlen() |
| 144 | //===----------------------------------------------------------------------=== |
| 145 | |
| 146 | #define strnlen BUILTIN(strnlen) |
| 147 | size_t strnlen(const char *s, size_t maxlen); |
| 148 | |
| 149 | void strnlen_constant0() { |
| 150 | if (strnlen("123", 10) != 3) |
| 151 | (void)*(char*)0; // no-warning |
| 152 | } |
| 153 | |
| 154 | void strnlen_constant1() { |
| 155 | const char *a = "123"; |
| 156 | if (strnlen(a, 10) != 3) |
| 157 | (void)*(char*)0; // no-warning |
| 158 | } |
| 159 | |
| 160 | void strnlen_constant2(char x) { |
| 161 | char a[] = "123"; |
| 162 | if (strnlen(a, 10) != 3) |
| 163 | (void)*(char*)0; // no-warning |
| 164 | a[0] = x; |
| 165 | if (strnlen(a, 10) != 3) |
| 166 | (void)*(char*)0; // expected-warning{{null}} |
| 167 | } |
| 168 | |
| 169 | void strnlen_constant4() { |
| 170 | if (strnlen("123456", 3) != 3) |
| 171 | (void)*(char*)0; // no-warning |
| 172 | } |
| 173 | |
| 174 | void strnlen_constant5() { |
| 175 | const char *a = "123456"; |
| 176 | if (strnlen(a, 3) != 3) |
| 177 | (void)*(char*)0; // no-warning |
| 178 | } |
| 179 | |
| 180 | void strnlen_constant6(char x) { |
| 181 | char a[] = "123456"; |
| 182 | if (strnlen(a, 3) != 3) |
| 183 | (void)*(char*)0; // no-warning |
| 184 | a[0] = x; |
| 185 | if (strnlen(a, 3) != 3) |
| 186 | (void)*(char*)0; // expected-warning{{null}} |
| 187 | } |
| 188 | |
| 189 | size_t strnlen_null() { |
| 190 | return strnlen(0, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 191 | } |
| 192 | |
| 193 | size_t strnlen_fn() { |
| 194 | return strnlen((char*)&strlen_fn, 3); // expected-warning{{Argument to byte string function is the address of the function 'strlen_fn', which is not a null-terminated string}} |
| 195 | } |
| 196 | |
| 197 | size_t strnlen_nonloc() { |
| 198 | label: |
| 199 | return strnlen((char*)&&label, 3); // expected-warning{{Argument to byte string function is the address of the label 'label', which is not a null-terminated string}} |
| 200 | } |
| 201 | |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 202 | void strnlen_zero() { |
| 203 | if (strnlen("abc", 0) != 0) |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 204 | (void)*(char*)0; // no-warning |
Jordy Rose | 793bff3 | 2011-06-14 01:15:31 +0000 | [diff] [blame] | 205 | if (strnlen(NULL, 0) != 0) // no-warning |
| 206 | (void)*(char*)0; // no-warning |
| 207 | } |
| 208 | |
| 209 | size_t strnlen_compound_literal() { |
| 210 | // This used to crash because we don't model the string lengths of |
| 211 | // compound literals. |
| 212 | return strnlen((char[]) { 'a', 'b', 0 }, 1); |
| 213 | } |
| 214 | |
| 215 | size_t strnlen_unknown_limit(float f) { |
| 216 | // This used to crash because we don't model the integer values of floats. |
| 217 | return strnlen("abc", (int)f); |
| 218 | } |
| 219 | |
| 220 | void strnlen_is_not_strlen(char *x) { |
| 221 | if (strnlen(x, 10) != strlen(x)) |
| 222 | (void)*(char*)0; // expected-warning{{null}} |
| 223 | } |
| 224 | |
| 225 | void strnlen_at_limit(char *x) { |
| 226 | size_t len = strnlen(x, 10); |
| 227 | if (len > 10) |
| 228 | (void)*(char*)0; // expected-warning{{never executed}} |
| 229 | if (len == 10) |
| 230 | (void)*(char*)0; // expected-warning{{null}} |
| 231 | } |
| 232 | |
| 233 | void strnlen_less_than_limit(char *x) { |
| 234 | size_t len = strnlen(x, 10); |
| 235 | if (len > 10) |
| 236 | (void)*(char*)0; // expected-warning{{never executed}} |
| 237 | if (len < 10) |
| 238 | (void)*(char*)0; // expected-warning{{null}} |
| 239 | } |
| 240 | |
| 241 | void strnlen_at_actual(size_t limit) { |
| 242 | size_t len = strnlen("abc", limit); |
| 243 | if (len > 3) |
| 244 | (void)*(char*)0; // expected-warning{{never executed}} |
| 245 | if (len == 3) |
| 246 | (void)*(char*)0; // expected-warning{{null}} |
| 247 | } |
| 248 | |
| 249 | void strnlen_less_than_actual(size_t limit) { |
| 250 | size_t len = strnlen("abc", limit); |
| 251 | if (len > 3) |
| 252 | (void)*(char*)0; // expected-warning{{never executed}} |
| 253 | if (len < 3) |
| 254 | (void)*(char*)0; // expected-warning{{null}} |
Ted Kremenek | be4242c | 2011-02-22 04:55:05 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | //===----------------------------------------------------------------------=== |
Jordy Rose | e64f311 | 2010-08-16 07:51:42 +0000 | [diff] [blame] | 258 | // strcpy() |
| 259 | //===----------------------------------------------------------------------=== |
| 260 | |
| 261 | #ifdef VARIANT |
| 262 | |
| 263 | #define __strcpy_chk BUILTIN(__strcpy_chk) |
| 264 | char *__strcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen); |
| 265 | |
| 266 | #define strcpy(a,b) __strcpy_chk(a,b,(size_t)-1) |
| 267 | |
| 268 | #else /* VARIANT */ |
| 269 | |
| 270 | #define strcpy BUILTIN(strcpy) |
| 271 | char *strcpy(char *restrict s1, const char *restrict s2); |
| 272 | |
| 273 | #endif /* VARIANT */ |
| 274 | |
| 275 | |
| 276 | void strcpy_null_dst(char *x) { |
| 277 | strcpy(NULL, x); // expected-warning{{Null pointer argument in call to byte string function}} |
| 278 | } |
| 279 | |
| 280 | void strcpy_null_src(char *x) { |
| 281 | strcpy(x, NULL); // expected-warning{{Null pointer argument in call to byte string function}} |
| 282 | } |
| 283 | |
| 284 | void strcpy_fn(char *x) { |
| 285 | strcpy(x, (char*)&strcpy_fn); // expected-warning{{Argument to byte string function is the address of the function 'strcpy_fn', which is not a null-terminated string}} |
| 286 | } |
| 287 | |
| 288 | void strcpy_effects(char *x, char *y) { |
| 289 | char a = x[0]; |
| 290 | |
| 291 | if (strcpy(x, y) != x) |
| 292 | (void)*(char*)0; // no-warning |
| 293 | |
| 294 | if (strlen(x) != strlen(y)) |
| 295 | (void)*(char*)0; // no-warning |
| 296 | |
| 297 | if (a != x[0]) |
| 298 | (void)*(char*)0; // expected-warning{{null}} |
| 299 | } |
| 300 | |
| 301 | void strcpy_overflow(char *y) { |
| 302 | char x[4]; |
| 303 | if (strlen(y) == 4) |
| 304 | strcpy(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 305 | } |
| 306 | |
| 307 | void strcpy_no_overflow(char *y) { |
| 308 | char x[4]; |
| 309 | if (strlen(y) == 3) |
| 310 | strcpy(x, y); // no-warning |
| 311 | } |
| 312 | |
| 313 | //===----------------------------------------------------------------------=== |
| 314 | // stpcpy() |
| 315 | //===----------------------------------------------------------------------=== |
| 316 | |
| 317 | #ifdef VARIANT |
| 318 | |
| 319 | #define __stpcpy_chk BUILTIN(__stpcpy_chk) |
| 320 | char *__stpcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen); |
| 321 | |
| 322 | #define stpcpy(a,b) __stpcpy_chk(a,b,(size_t)-1) |
| 323 | |
| 324 | #else /* VARIANT */ |
| 325 | |
| 326 | #define stpcpy BUILTIN(stpcpy) |
| 327 | char *stpcpy(char *restrict s1, const char *restrict s2); |
| 328 | |
| 329 | #endif /* VARIANT */ |
| 330 | |
| 331 | |
| 332 | void stpcpy_effect(char *x, char *y) { |
| 333 | char a = x[0]; |
| 334 | |
| 335 | if (stpcpy(x, y) != &x[strlen(y)]) |
| 336 | (void)*(char*)0; // no-warning |
| 337 | |
| 338 | if (strlen(x) != strlen(y)) |
| 339 | (void)*(char*)0; // no-warning |
| 340 | |
| 341 | if (a != x[0]) |
| 342 | (void)*(char*)0; // expected-warning{{null}} |
| 343 | } |
| 344 | |
| 345 | void stpcpy_overflow(char *y) { |
| 346 | char x[4]; |
| 347 | if (strlen(y) == 4) |
| 348 | stpcpy(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 349 | } |
| 350 | |
| 351 | void stpcpy_no_overflow(char *y) { |
| 352 | char x[4]; |
| 353 | if (strlen(y) == 3) |
| 354 | stpcpy(x, y); // no-warning |
| 355 | } |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 356 | |
| 357 | //===----------------------------------------------------------------------=== |
| 358 | // strcat() |
| 359 | //===----------------------------------------------------------------------=== |
| 360 | |
| 361 | #ifdef VARIANT |
| 362 | |
| 363 | #define __strcat_chk BUILTIN(__strcat_chk) |
| 364 | char *__strcat_chk(char *restrict s1, const char *restrict s2, size_t destlen); |
| 365 | |
| 366 | #define strcat(a,b) __strcat_chk(a,b,(size_t)-1) |
| 367 | |
| 368 | #else /* VARIANT */ |
| 369 | |
| 370 | #define strcat BUILTIN(strcat) |
| 371 | char *strcat(char *restrict s1, const char *restrict s2); |
| 372 | |
| 373 | #endif /* VARIANT */ |
| 374 | |
| 375 | |
| 376 | void strcat_null_dst(char *x) { |
| 377 | strcat(NULL, x); // expected-warning{{Null pointer argument in call to byte string function}} |
| 378 | } |
| 379 | |
| 380 | void strcat_null_src(char *x) { |
| 381 | strcat(x, NULL); // expected-warning{{Null pointer argument in call to byte string function}} |
| 382 | } |
| 383 | |
| 384 | void strcat_fn(char *x) { |
| 385 | strcat(x, (char*)&strcat_fn); // expected-warning{{Argument to byte string function is the address of the function 'strcat_fn', which is not a null-terminated string}} |
| 386 | } |
| 387 | |
| 388 | void strcat_effects(char *y) { |
| 389 | char x[8] = "123"; |
| 390 | size_t orig_len = strlen(x); |
| 391 | char a = x[0]; |
| 392 | |
| 393 | if (strlen(y) != 4) |
| 394 | return; |
| 395 | |
| 396 | if (strcat(x, y) != x) |
| 397 | (void)*(char*)0; // no-warning |
| 398 | |
| 399 | if ((int)strlen(x) != (orig_len + strlen(y))) |
| 400 | (void)*(char*)0; // no-warning |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void strcat_overflow_0(char *y) { |
| 404 | char x[4] = "12"; |
| 405 | if (strlen(y) == 4) |
| 406 | strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 407 | } |
| 408 | |
| 409 | void strcat_overflow_1(char *y) { |
| 410 | char x[4] = "12"; |
| 411 | if (strlen(y) == 3) |
| 412 | strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 413 | } |
| 414 | |
| 415 | void strcat_overflow_2(char *y) { |
| 416 | char x[4] = "12"; |
| 417 | if (strlen(y) == 2) |
| 418 | strcat(x, y); // expected-warning{{Byte string function overflows destination buffer}} |
| 419 | } |
| 420 | |
| 421 | void strcat_no_overflow(char *y) { |
| 422 | char x[5] = "12"; |
| 423 | if (strlen(y) == 2) |
| 424 | strcat(x, y); // no-warning |
| 425 | } |
| 426 | |
Jordy Rose | d5af0e1 | 2011-06-15 05:52:56 +0000 | [diff] [blame] | 427 | void strcat_symbolic_dst_length(char *dst) { |
| 428 | strcat(dst, "1234"); |
| 429 | if (strlen(dst) < 4) |
| 430 | (void)*(char*)0; // no-warning |
| 431 | } |
| 432 | |
| 433 | void strcat_symbolic_src_length(char *src) { |
| 434 | char dst[8] = "1234"; |
| 435 | strcat(dst, src); |
| 436 | if (strlen(dst) < 4) |
| 437 | (void)*(char*)0; // no-warning |
| 438 | } |
| 439 | |
| 440 | void strcat_unknown_src_length(char *src, int offset) { |
| 441 | char dst[8] = "1234"; |
| 442 | strcat(dst, &src[offset]); |
| 443 | if (strlen(dst) < 4) |
| 444 | (void)*(char*)0; // no-warning |
| 445 | } |
| 446 | |
| 447 | // There is no strcat_unknown_dst_length because if we can't get a symbolic |
| 448 | // length for the "before" strlen, we won't be able to set one for "after". |
| 449 | |
| 450 | void strcat_too_big(char *dst, char *src) { |
| 451 | if (strlen(dst) != (((size_t)0) - 2)) |
| 452 | return; |
| 453 | if (strlen(src) != 2) |
| 454 | return; |
| 455 | strcat(dst, src); // expected-warning{{This expression will create a string whose length is too big to be represented as a size_t}} |
| 456 | } |
| 457 | |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 458 | |
| 459 | //===----------------------------------------------------------------------=== |
| 460 | // strncat() |
| 461 | //===----------------------------------------------------------------------=== |
| 462 | |
| 463 | #ifdef VARIANT |
| 464 | |
| 465 | #define __strncat_chk BUILTIN(__strncat_chk) |
| 466 | char *__strncat_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen); |
| 467 | |
| 468 | #define strncat(a,b,c) __strncat_chk(a,b,c, (size_t)-1) |
| 469 | |
| 470 | #else /* VARIANT */ |
| 471 | |
| 472 | #define strncat BUILTIN(strncat) |
| 473 | char *strncat(char *restrict s1, const char *restrict s2, size_t n); |
| 474 | |
| 475 | #endif /* VARIANT */ |
| 476 | |
| 477 | |
| 478 | void strncat_null_dst(char *x) { |
| 479 | strncat(NULL, x, 4); // expected-warning{{Null pointer argument in call to byte string function}} |
| 480 | } |
| 481 | |
| 482 | void strncat_null_src(char *x) { |
| 483 | strncat(x, NULL, 4); // expected-warning{{Null pointer argument in call to byte string function}} |
| 484 | } |
| 485 | |
| 486 | void strncat_fn(char *x) { |
| 487 | strncat(x, (char*)&strncat_fn, 4); // expected-warning{{Argument to byte string function is the address of the function 'strncat_fn', which is not a null-terminated string}} |
| 488 | } |
| 489 | |
| 490 | void strncat_effects(char *y) { |
| 491 | char x[8] = "123"; |
| 492 | size_t orig_len = strlen(x); |
| 493 | char a = x[0]; |
| 494 | |
| 495 | if (strlen(y) != 4) |
| 496 | return; |
| 497 | |
| 498 | if (strncat(x, y, strlen(y)) != x) |
| 499 | (void)*(char*)0; // no-warning |
| 500 | |
| 501 | if (strlen(x) != orig_len + strlen(y)) |
| 502 | (void)*(char*)0; // no-warning |
Lenny Maiorani | 067bbd0 | 2011-04-09 15:12:58 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void strncat_overflow_0(char *y) { |
| 506 | char x[4] = "12"; |
| 507 | if (strlen(y) == 4) |
| 508 | strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} |
| 509 | } |
| 510 | |
| 511 | void strncat_overflow_1(char *y) { |
| 512 | char x[4] = "12"; |
| 513 | if (strlen(y) == 3) |
| 514 | strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} |
| 515 | } |
| 516 | |
| 517 | void strncat_overflow_2(char *y) { |
| 518 | char x[4] = "12"; |
| 519 | if (strlen(y) == 2) |
| 520 | strncat(x, y, strlen(y)); // expected-warning{{Byte string function overflows destination buffer}} |
| 521 | } |
| 522 | |
| 523 | void strncat_overflow_3(char *y) { |
| 524 | char x[4] = "12"; |
| 525 | if (strlen(y) == 4) |
| 526 | strncat(x, y, 2); // expected-warning{{Byte string function overflows destination buffer}} |
| 527 | } |
| 528 | void strncat_no_overflow_1(char *y) { |
| 529 | char x[5] = "12"; |
| 530 | if (strlen(y) == 2) |
| 531 | strncat(x, y, strlen(y)); // no-warning |
| 532 | } |
| 533 | |
| 534 | void strncat_no_overflow_2(char *y) { |
| 535 | char x[4] = "12"; |
| 536 | if (strlen(y) == 4) |
| 537 | strncat(x, y, 1); // no-warning |
| 538 | } |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 539 | |
| 540 | //===----------------------------------------------------------------------=== |
| 541 | // strcmp() |
| 542 | //===----------------------------------------------------------------------=== |
| 543 | |
| 544 | #define strcmp BUILTIN(strcmp) |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame^] | 545 | int strcmp(const char * s1, const char * s2); |
Lenny Maiorani | 318dd92 | 2011-04-12 17:08:43 +0000 | [diff] [blame] | 546 | |
| 547 | void strcmp_constant0() { |
| 548 | if (strcmp("123", "123") != 0) |
| 549 | (void)*(char*)0; // no-warning |
| 550 | } |
| 551 | |
| 552 | void strcmp_constant_and_var_0() { |
| 553 | char *x = "123"; |
| 554 | if (strcmp(x, "123") != 0) |
| 555 | (void)*(char*)0; // no-warning |
| 556 | } |
| 557 | |
| 558 | void strcmp_constant_and_var_1() { |
| 559 | char *x = "123"; |
| 560 | if (strcmp("123", x) != 0) |
| 561 | (void)*(char*)0; // no-warning |
| 562 | } |
| 563 | |
| 564 | void strcmp_0() { |
| 565 | char *x = "123"; |
| 566 | char *y = "123"; |
| 567 | if (strcmp(x, y) != 0) |
| 568 | (void)*(char*)0; // no-warning |
| 569 | } |
| 570 | |
| 571 | void strcmp_1() { |
| 572 | char *x = "234"; |
| 573 | char *y = "123"; |
| 574 | if (strcmp(x, y) != 1) |
| 575 | (void)*(char*)0; // no-warning |
| 576 | } |
| 577 | |
| 578 | void strcmp_2() { |
| 579 | char *x = "123"; |
| 580 | char *y = "234"; |
| 581 | if (strcmp(x, y) != -1) |
| 582 | (void)*(char*)0; // no-warning |
| 583 | } |
| 584 | |
| 585 | void strcmp_null_0() { |
| 586 | char *x = NULL; |
| 587 | char *y = "123"; |
| 588 | strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} |
| 589 | } |
| 590 | |
| 591 | void strcmp_null_1() { |
| 592 | char *x = "123"; |
| 593 | char *y = NULL; |
| 594 | strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} |
| 595 | } |
| 596 | |
| 597 | void strcmp_diff_length_0() { |
| 598 | char *x = "12345"; |
| 599 | char *y = "234"; |
| 600 | if (strcmp(x, y) != -1) |
| 601 | (void)*(char*)0; // no-warning |
| 602 | } |
| 603 | |
| 604 | void strcmp_diff_length_1() { |
| 605 | char *x = "123"; |
| 606 | char *y = "23456"; |
| 607 | if (strcmp(x, y) != -1) |
| 608 | (void)*(char*)0; // no-warning |
| 609 | } |
| 610 | |
| 611 | void strcmp_diff_length_2() { |
| 612 | char *x = "12345"; |
| 613 | char *y = "123"; |
| 614 | if (strcmp(x, y) != 1) |
| 615 | (void)*(char*)0; // no-warning |
| 616 | } |
| 617 | |
| 618 | void strcmp_diff_length_3() { |
| 619 | char *x = "123"; |
| 620 | char *y = "12345"; |
| 621 | if (strcmp(x, y) != -1) |
| 622 | (void)*(char*)0; // no-warning |
| 623 | } |
| 624 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame^] | 625 | void strcmp_embedded_null () { |
| 626 | if (strcmp("\0z", "\0y") != 0) |
| 627 | (void)*(char*)0; // no-warning |
| 628 | } |
| 629 | |
| 630 | void strcmp_unknown_arg (char *unknown) { |
| 631 | if (strcmp(unknown, unknown) != 0) |
| 632 | (void)*(char*)0; // no-warning |
| 633 | } |
| 634 | |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 635 | //===----------------------------------------------------------------------=== |
| 636 | // strncmp() |
| 637 | //===----------------------------------------------------------------------=== |
| 638 | |
| 639 | #define strncmp BUILTIN(strncmp) |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame^] | 640 | int strncmp(const char *s1, const char *s2, size_t n); |
Lenny Maiorani | 357f6ee | 2011-04-25 22:21:00 +0000 | [diff] [blame] | 641 | |
| 642 | void strncmp_constant0() { |
| 643 | if (strncmp("123", "123", 3) != 0) |
| 644 | (void)*(char*)0; // no-warning |
| 645 | } |
| 646 | |
| 647 | void strncmp_constant_and_var_0() { |
| 648 | char *x = "123"; |
| 649 | if (strncmp(x, "123", 3) != 0) |
| 650 | (void)*(char*)0; // no-warning |
| 651 | } |
| 652 | |
| 653 | void strncmp_constant_and_var_1() { |
| 654 | char *x = "123"; |
| 655 | if (strncmp("123", x, 3) != 0) |
| 656 | (void)*(char*)0; // no-warning |
| 657 | } |
| 658 | |
| 659 | void strncmp_0() { |
| 660 | char *x = "123"; |
| 661 | char *y = "123"; |
| 662 | if (strncmp(x, y, 3) != 0) |
| 663 | (void)*(char*)0; // no-warning |
| 664 | } |
| 665 | |
| 666 | void strncmp_1() { |
| 667 | char *x = "234"; |
| 668 | char *y = "123"; |
| 669 | if (strncmp(x, y, 3) != 1) |
| 670 | (void)*(char*)0; // no-warning |
| 671 | } |
| 672 | |
| 673 | void strncmp_2() { |
| 674 | char *x = "123"; |
| 675 | char *y = "234"; |
| 676 | if (strncmp(x, y, 3) != -1) |
| 677 | (void)*(char*)0; // no-warning |
| 678 | } |
| 679 | |
| 680 | void strncmp_null_0() { |
| 681 | char *x = NULL; |
| 682 | char *y = "123"; |
| 683 | strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 684 | } |
| 685 | |
| 686 | void strncmp_null_1() { |
| 687 | char *x = "123"; |
| 688 | char *y = NULL; |
| 689 | strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 690 | } |
| 691 | |
| 692 | void strncmp_diff_length_0() { |
| 693 | char *x = "12345"; |
| 694 | char *y = "234"; |
| 695 | if (strncmp(x, y, 5) != -1) |
| 696 | (void)*(char*)0; // no-warning |
| 697 | } |
| 698 | |
| 699 | void strncmp_diff_length_1() { |
| 700 | char *x = "123"; |
| 701 | char *y = "23456"; |
| 702 | if (strncmp(x, y, 5) != -1) |
| 703 | (void)*(char*)0; // no-warning |
| 704 | } |
| 705 | |
| 706 | void strncmp_diff_length_2() { |
| 707 | char *x = "12345"; |
| 708 | char *y = "123"; |
| 709 | if (strncmp(x, y, 5) != 1) |
| 710 | (void)*(char*)0; // no-warning |
| 711 | } |
| 712 | |
| 713 | void strncmp_diff_length_3() { |
| 714 | char *x = "123"; |
| 715 | char *y = "12345"; |
| 716 | if (strncmp(x, y, 5) != -1) |
| 717 | (void)*(char*)0; // no-warning |
| 718 | } |
| 719 | |
| 720 | void strncmp_diff_length_4() { |
| 721 | char *x = "123"; |
| 722 | char *y = "12345"; |
| 723 | if (strncmp(x, y, 3) != 0) |
| 724 | (void)*(char*)0; // no-warning |
| 725 | } |
| 726 | |
| 727 | void strncmp_diff_length_5() { |
| 728 | char *x = "012"; |
| 729 | char *y = "12345"; |
| 730 | if (strncmp(x, y, 3) != -1) |
| 731 | (void)*(char*)0; // no-warning |
| 732 | } |
| 733 | |
| 734 | void strncmp_diff_length_6() { |
| 735 | char *x = "234"; |
| 736 | char *y = "12345"; |
| 737 | if (strncmp(x, y, 3) != 1) |
| 738 | (void)*(char*)0; // no-warning |
| 739 | } |
| 740 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame^] | 741 | void strncmp_embedded_null () { |
| 742 | if (strncmp("ab\0zz", "ab\0yy", 4) != 0) |
| 743 | (void)*(char*)0; // no-warning |
| 744 | } |
| 745 | |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 746 | //===----------------------------------------------------------------------=== |
| 747 | // strcasecmp() |
| 748 | //===----------------------------------------------------------------------=== |
| 749 | |
| 750 | #define strcasecmp BUILTIN(strcasecmp) |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame^] | 751 | int strcasecmp(const char *s1, const char *s2); |
Lenny Maiorani | bd1d16a | 2011-04-28 15:09:11 +0000 | [diff] [blame] | 752 | |
| 753 | void strcasecmp_constant0() { |
| 754 | if (strcasecmp("abc", "Abc") != 0) |
| 755 | (void)*(char*)0; // no-warning |
| 756 | } |
| 757 | |
| 758 | void strcasecmp_constant_and_var_0() { |
| 759 | char *x = "abc"; |
| 760 | if (strcasecmp(x, "Abc") != 0) |
| 761 | (void)*(char*)0; // no-warning |
| 762 | } |
| 763 | |
| 764 | void strcasecmp_constant_and_var_1() { |
| 765 | char *x = "abc"; |
| 766 | if (strcasecmp("Abc", x) != 0) |
| 767 | (void)*(char*)0; // no-warning |
| 768 | } |
| 769 | |
| 770 | void strcasecmp_0() { |
| 771 | char *x = "abc"; |
| 772 | char *y = "Abc"; |
| 773 | if (strcasecmp(x, y) != 0) |
| 774 | (void)*(char*)0; // no-warning |
| 775 | } |
| 776 | |
| 777 | void strcasecmp_1() { |
| 778 | char *x = "Bcd"; |
| 779 | char *y = "abc"; |
| 780 | if (strcasecmp(x, y) != 1) |
| 781 | (void)*(char*)0; // no-warning |
| 782 | } |
| 783 | |
| 784 | void strcasecmp_2() { |
| 785 | char *x = "abc"; |
| 786 | char *y = "Bcd"; |
| 787 | if (strcasecmp(x, y) != -1) |
| 788 | (void)*(char*)0; // no-warning |
| 789 | } |
| 790 | |
| 791 | void strcasecmp_null_0() { |
| 792 | char *x = NULL; |
| 793 | char *y = "123"; |
| 794 | strcasecmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} |
| 795 | } |
| 796 | |
| 797 | void strcasecmp_null_1() { |
| 798 | char *x = "123"; |
| 799 | char *y = NULL; |
| 800 | strcasecmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} |
| 801 | } |
| 802 | |
| 803 | void strcasecmp_diff_length_0() { |
| 804 | char *x = "abcde"; |
| 805 | char *y = "aBd"; |
| 806 | if (strcasecmp(x, y) != -1) |
| 807 | (void)*(char*)0; // no-warning |
| 808 | } |
| 809 | |
| 810 | void strcasecmp_diff_length_1() { |
| 811 | char *x = "abc"; |
| 812 | char *y = "aBdef"; |
| 813 | if (strcasecmp(x, y) != -1) |
| 814 | (void)*(char*)0; // no-warning |
| 815 | } |
| 816 | |
| 817 | void strcasecmp_diff_length_2() { |
| 818 | char *x = "aBcDe"; |
| 819 | char *y = "abc"; |
| 820 | if (strcasecmp(x, y) != 1) |
| 821 | (void)*(char*)0; // no-warning |
| 822 | } |
| 823 | |
| 824 | void strcasecmp_diff_length_3() { |
| 825 | char *x = "aBc"; |
| 826 | char *y = "abcde"; |
| 827 | if (strcasecmp(x, y) != -1) |
| 828 | (void)*(char*)0; // no-warning |
| 829 | } |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 830 | |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame^] | 831 | void strcasecmp_embedded_null () { |
| 832 | if (strcasecmp("ab\0zz", "ab\0yy") != 0) |
| 833 | (void)*(char*)0; // no-warning |
| 834 | } |
| 835 | |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 836 | //===----------------------------------------------------------------------=== |
| 837 | // strncasecmp() |
| 838 | //===----------------------------------------------------------------------=== |
| 839 | |
| 840 | #define strncasecmp BUILTIN(strncasecmp) |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame^] | 841 | int strncasecmp(const char *s1, const char *s2, size_t n); |
Lenny Maiorani | 454fd2d | 2011-05-02 19:05:49 +0000 | [diff] [blame] | 842 | |
| 843 | void strncasecmp_constant0() { |
| 844 | if (strncasecmp("abc", "Abc", 3) != 0) |
| 845 | (void)*(char*)0; // no-warning |
| 846 | } |
| 847 | |
| 848 | void strncasecmp_constant_and_var_0() { |
| 849 | char *x = "abc"; |
| 850 | if (strncasecmp(x, "Abc", 3) != 0) |
| 851 | (void)*(char*)0; // no-warning |
| 852 | } |
| 853 | |
| 854 | void strncasecmp_constant_and_var_1() { |
| 855 | char *x = "abc"; |
| 856 | if (strncasecmp("Abc", x, 3) != 0) |
| 857 | (void)*(char*)0; // no-warning |
| 858 | } |
| 859 | |
| 860 | void strncasecmp_0() { |
| 861 | char *x = "abc"; |
| 862 | char *y = "Abc"; |
| 863 | if (strncasecmp(x, y, 3) != 0) |
| 864 | (void)*(char*)0; // no-warning |
| 865 | } |
| 866 | |
| 867 | void strncasecmp_1() { |
| 868 | char *x = "Bcd"; |
| 869 | char *y = "abc"; |
| 870 | if (strncasecmp(x, y, 3) != 1) |
| 871 | (void)*(char*)0; // no-warning |
| 872 | } |
| 873 | |
| 874 | void strncasecmp_2() { |
| 875 | char *x = "abc"; |
| 876 | char *y = "Bcd"; |
| 877 | if (strncasecmp(x, y, 3) != -1) |
| 878 | (void)*(char*)0; // no-warning |
| 879 | } |
| 880 | |
| 881 | void strncasecmp_null_0() { |
| 882 | char *x = NULL; |
| 883 | char *y = "123"; |
| 884 | strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 885 | } |
| 886 | |
| 887 | void strncasecmp_null_1() { |
| 888 | char *x = "123"; |
| 889 | char *y = NULL; |
| 890 | strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to byte string function}} |
| 891 | } |
| 892 | |
| 893 | void strncasecmp_diff_length_0() { |
| 894 | char *x = "abcde"; |
| 895 | char *y = "aBd"; |
| 896 | if (strncasecmp(x, y, 5) != -1) |
| 897 | (void)*(char*)0; // no-warning |
| 898 | } |
| 899 | |
| 900 | void strncasecmp_diff_length_1() { |
| 901 | char *x = "abc"; |
| 902 | char *y = "aBdef"; |
| 903 | if (strncasecmp(x, y, 5) != -1) |
| 904 | (void)*(char*)0; // no-warning |
| 905 | } |
| 906 | |
| 907 | void strncasecmp_diff_length_2() { |
| 908 | char *x = "aBcDe"; |
| 909 | char *y = "abc"; |
| 910 | if (strncasecmp(x, y, 5) != 1) |
| 911 | (void)*(char*)0; // no-warning |
| 912 | } |
| 913 | |
| 914 | void strncasecmp_diff_length_3() { |
| 915 | char *x = "aBc"; |
| 916 | char *y = "abcde"; |
| 917 | if (strncasecmp(x, y, 5) != -1) |
| 918 | (void)*(char*)0; // no-warning |
| 919 | } |
| 920 | |
| 921 | void strncasecmp_diff_length_4() { |
| 922 | char *x = "abcde"; |
| 923 | char *y = "aBc"; |
| 924 | if (strncasecmp(x, y, 3) != 0) |
| 925 | (void)*(char*)0; // no-warning |
| 926 | } |
| 927 | |
| 928 | void strncasecmp_diff_length_5() { |
| 929 | char *x = "abcde"; |
| 930 | char *y = "aBd"; |
| 931 | if (strncasecmp(x, y, 3) != -1) |
| 932 | (void)*(char*)0; // no-warning |
| 933 | } |
| 934 | |
| 935 | void strncasecmp_diff_length_6() { |
| 936 | char *x = "aBDe"; |
| 937 | char *y = "abc"; |
| 938 | if (strncasecmp(x, y, 3) != 1) |
| 939 | (void)*(char*)0; // no-warning |
| 940 | } |
Jordy Rose | adc42d4 | 2011-06-16 07:13:34 +0000 | [diff] [blame^] | 941 | |
| 942 | void strncasecmp_embedded_null () { |
| 943 | if (strncasecmp("ab\0zz", "ab\0yy", 4) != 0) |
| 944 | (void)*(char*)0; // no-warning |
| 945 | } |