blob: f01de36c1afb8fd62fb5dc8fcdbca4cd5e9f5c7d [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// 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
David Carlierc30cedf2018-07-20 20:39:49 +00002// 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
Anna Zaks87b6ff02012-01-31 19:33:39 +00005
6typedef __SIZE_TYPE__ size_t;
7char *strncat(char *, const char *, size_t);
8size_t strlen (const char *s);
David Carlier8e75de22018-07-19 21:50:03 +00009size_t strlcpy(char *, const char *, size_t);
David Carlier75cb0dd2018-09-23 08:30:17 +000010size_t strlcat(char *, const char *, size_t);
Anna Zaks87b6ff02012-01-31 19:33:39 +000011
12void testStrncat(const char *src) {
13 char dest[10];
14 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest) - 1); // expected-warning {{Potential buffer overflow. Replace with 'sizeof(dest) - strlen(dest) - 1' or use a safer 'strlcat' API}}
15 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest)); // expected-warning {{Potential buffer overflow. Replace with}}
16 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest) - strlen(dest)); // expected-warning {{Potential buffer overflow. Replace with}}
17 strncat(dest, src, sizeof(src)); // expected-warning {{Potential buffer overflow. Replace with}}
Gabor Horvath3b008532017-02-02 08:20:54 +000018 // Should not crash when sizeof has a type argument.
19 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(char));
Anna Zaks87b6ff02012-01-31 19:33:39 +000020}
David Carlier8e75de22018-07-19 21:50:03 +000021
22void testStrlcpy(const char *src) {
23 char dest[10];
24 size_t destlen = sizeof(dest);
25 size_t srclen = sizeof(src);
26 size_t badlen = 20;
27 size_t ulen;
28 strlcpy(dest, src, sizeof(dest));
29 strlcpy(dest, src, destlen);
30 strlcpy(dest, src, 10);
David Carlier75cb0dd2018-09-23 08:30:17 +000031 strlcpy(dest, src, 20); // expected-warning {{The third argument allows to potentially copy more bytes than it should. Replace with the value sizeof(dest) or lower}}
32 strlcpy(dest, src, badlen); // expected-warning {{The third argument allows to potentially copy more bytes than it should. Replace with the value sizeof(dest) or lower}}
David Carlier8e75de22018-07-19 21:50:03 +000033 strlcpy(dest, src, ulen);
David Carlierc30cedf2018-07-20 20:39:49 +000034 strlcpy(dest + 5, src, 5);
David Carlier75cb0dd2018-09-23 08:30:17 +000035 strlcpy(dest + 5, src, 10); // expected-warning {{The third argument allows to potentially copy more bytes than it should. Replace with the value sizeof(<destination buffer>) or lower}}
Artem Dergachev91970562019-02-08 23:59:52 +000036 strlcpy(dest, "aaaaaaaaaaaaaaa", 10); // no-warning
David Carlier75cb0dd2018-09-23 08:30:17 +000037}
38
39void testStrlcat(const char *src) {
40 char dest[10];
41 size_t badlen = 20;
42 size_t ulen;
43 strlcpy(dest, "aaaaa", sizeof("aaaaa") - 1);
44 strlcat(dest, "bbbb", (sizeof("bbbb") - 1) - sizeof(dest) - 1);
45 strlcpy(dest, "012345678", sizeof(dest));
46 strlcat(dest, "910", sizeof(dest));
47 strlcpy(dest, "0123456789", sizeof(dest));
48 strlcpy(dest, "0123456789", sizeof(dest));
49 strlcat(dest, "0123456789", badlen / 2);
50 strlcat(dest, "0123456789", badlen); // expected-warning {{The third argument allows to potentially copy more bytes than it should. Replace with the value sizeof(dest) or lower}}
51 strlcat(dest, "0123456789", badlen - strlen(dest) - 1);
52 strlcat(dest, src, ulen);
53 strlcpy(dest, src, 5);
54 strlcat(dest + 5, src, badlen); // expected-warning {{The third argument allows to potentially copy more bytes than it should. Replace with the value sizeof(<destination buffer>) or lower}}
Artem Dergachev91970562019-02-08 23:59:52 +000055 strlcat(dest, "aaaaaaaaaaaaaaa", 10); // no-warning
David Carlier8e75de22018-07-19 21:50:03 +000056}