1 // RUN: %clang_analyze_cc1 -analyzer-checker=unix.cstring.BadSizeArg -analyzer-store=region -Wno-strncat-size -Wno-strlcpy-strlcat-size -Wno-sizeof-array-argument -Wno-sizeof-pointer-memaccess -verify %s 2 // RUN: %clang_analyze_cc1 -triple armv7-a15-linux -analyzer-checker=unix.cstring.BadSizeArg -analyzer-store=region -Wno-strncat-size -Wno-strlcpy-strlcat-size -Wno-sizeof-array-argument -Wno-sizeof-pointer-memaccess -verify %s 3 // RUN: %clang_analyze_cc1 -triple aarch64_be-none-linux-gnu -analyzer-checker=unix.cstring.BadSizeArg -analyzer-store=region -Wno-strncat-size -Wno-strlcpy-strlcat-size -Wno-sizeof-array-argument -Wno-sizeof-pointer-memaccess -verify %s 4 // RUN: %clang_analyze_cc1 -triple i386-apple-darwin10 -analyzer-checker=unix.cstring.BadSizeArg -analyzer-store=region -Wno-strncat-size -Wno-strlcpy-strlcat-size -Wno-sizeof-array-argument -Wno-sizeof-pointer-memaccess -verify %s 5 6 typedef __SIZE_TYPE__ size_t; 7 char *strncat(char *, const char *, size_t); 8 size_t strlen (const char *s); 9 size_t strlcpy(char *, const char *, size_t); 10 11 void testStrncat(const char *src) { 12 char dest[10]; 13 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest) - 1); // expected-warning {{Potential buffer overflow. Replace with 'sizeof(dest) - strlen(dest) - 1' or use a safer 'strlcat' API}} 14 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest)); // expected-warning {{Potential buffer overflow. Replace with}} 15 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest) - strlen(dest)); // expected-warning {{Potential buffer overflow. Replace with}} 16 strncat(dest, src, sizeof(src)); // expected-warning {{Potential buffer overflow. Replace with}} 17 // Should not crash when sizeof has a type argument. 18 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(char)); 19 } 20 21 void testStrlcpy(const char *src) { 22 char dest[10]; 23 size_t destlen = sizeof(dest); 24 size_t srclen = sizeof(src); 25 size_t badlen = 20; 26 size_t ulen; 27 strlcpy(dest, src, sizeof(dest)); 28 strlcpy(dest, src, destlen); 29 strlcpy(dest, src, 10); 30 strlcpy(dest, src, 20); // expected-warning {{The third argument is larger than the size of the input buffer. Replace with the value 'sizeof(dest)` or lower}} 31 strlcpy(dest, src, badlen); // expected-warning {{The third argument is larger than the size of the input buffer. Replace with the value 'sizeof(dest)` or lower}} 32 strlcpy(dest, src, ulen); 33 strlcpy(dest + 5, src, 5); 34 strlcpy(dest + 5, src, 10); // expected-warning {{The third argument is larger than the size of the input buffer.}} 35 } 36