annotate clang/test/Sema/warn-strncat-size.c @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 1d019706d866
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -Wstrncat-size -verify -fsyntax-only %s
anatofuz
parents:
diff changeset
2 // RUN: %clang_cc1 -DUSE_BUILTINS -Wstrncat-size -verify -fsyntax-only %s
anatofuz
parents:
diff changeset
3 // RUN: %clang_cc1 -fsyntax-only -Wstrncat-size -fixit -x c %s
anatofuz
parents:
diff changeset
4 // RUN: %clang_cc1 -DUSE_BUILTINS -fsyntax-only -Wstrncat-size -fixit -x c %s
anatofuz
parents:
diff changeset
5
anatofuz
parents:
diff changeset
6 typedef __SIZE_TYPE__ size_t;
anatofuz
parents:
diff changeset
7 size_t strlen (const char *s);
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #ifdef USE_BUILTINS
anatofuz
parents:
diff changeset
10 # define BUILTIN(f) __builtin_ ## f
anatofuz
parents:
diff changeset
11 #else
anatofuz
parents:
diff changeset
12 # define BUILTIN(f) f
anatofuz
parents:
diff changeset
13 #endif
anatofuz
parents:
diff changeset
14
anatofuz
parents:
diff changeset
15 #define strncat BUILTIN(strncat)
anatofuz
parents:
diff changeset
16 char *strncat(char *restrict s1, const char *restrict s2, size_t n);
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 struct {
anatofuz
parents:
diff changeset
19 char f1[100];
anatofuz
parents:
diff changeset
20 char f2[100][3];
anatofuz
parents:
diff changeset
21 } s4, **s5;
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 char s1[100];
anatofuz
parents:
diff changeset
24 char s2[200];
anatofuz
parents:
diff changeset
25 int x;
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 void test(char *src) {
anatofuz
parents:
diff changeset
28 char dest[10];
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest) - strlen(dest) - 1); // no-warning
anatofuz
parents:
diff changeset
31 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest) - 1); // no-warning - the code might assume that dest is empty
anatofuz
parents:
diff changeset
32
anatofuz
parents:
diff changeset
33 strncat(dest, src, sizeof(src)); // expected-warning {{size argument in 'strncat' call appears to be size of the source}} expected-note {{change the argument to be the free space in the destination buffer minus the terminating null byte}}
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 strncat(dest, src, sizeof(src) - 1); // expected-warning {{size argument in 'strncat' call appears to be size of the source}} expected-note {{change the argument to be the free space in the destination buffer minus the terminating null byte}}
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest)); // expected-warning{{the value of the size argument in 'strncat' is too large, might lead to a buffer overflow}} expected-note {{change the argument to be the free space in the destination buffer minus the terminating null byte}}
anatofuz
parents:
diff changeset
38
anatofuz
parents:
diff changeset
39 strncat(dest, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", sizeof(dest) - strlen(dest)); // expected-warning{{the value of the size argument in 'strncat' is too large, might lead to a buffer overflow}} expected-note {{change the argument to be the free space in the destination buffer minus the terminating null byte}}
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 strncat((*s5)->f2[x], s2, sizeof(s2)); // expected-warning {{size argument in 'strncat' call appears to be size of the source}} expected-note {{change the argument to be the free space in the destination buffer minus the terminating null byte}}
anatofuz
parents:
diff changeset
42 strncat(s1+3, s2, sizeof(s2)); // expected-warning {{size argument in 'strncat' call appears to be size of the source}} expected-warning {{strncat' size argument is too large; destination buffer has size 97, but size argument is 200}}
anatofuz
parents:
diff changeset
43 strncat(s4.f1, s2, sizeof(s2)); // expected-warning {{size argument in 'strncat' call appears to be size of the source}} expected-note {{change the argument to be the free space in the destination buffer minus the terminating null byte}}
anatofuz
parents:
diff changeset
44 }
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 // Don't issue FIXIT for flexible arrays.
anatofuz
parents:
diff changeset
47 struct S {
anatofuz
parents:
diff changeset
48 int y;
anatofuz
parents:
diff changeset
49 char x[];
anatofuz
parents:
diff changeset
50 };
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 void flexible_arrays(struct S *s) {
anatofuz
parents:
diff changeset
53 char str[] = "hi";
anatofuz
parents:
diff changeset
54 strncat(s->x, str, sizeof(str)); // expected-warning {{size argument in 'strncat' call appears to be size of the source}}
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 // Don't issue FIXIT for destinations of size 1.
anatofuz
parents:
diff changeset
58 void size_1() {
anatofuz
parents:
diff changeset
59 char z[1];
anatofuz
parents:
diff changeset
60 char str[] = "hi";
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 strncat(z, str, sizeof(z)); // expected-warning{{the value of the size argument to 'strncat' is wrong}}
anatofuz
parents:
diff changeset
63 }
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 // Support VLAs.
anatofuz
parents:
diff changeset
66 void vlas(int size) {
anatofuz
parents:
diff changeset
67 char z[size];
anatofuz
parents:
diff changeset
68 char str[] = "hi";
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 strncat(z, str, sizeof(str)); // expected-warning {{size argument in 'strncat' call appears to be size of the source}} expected-note {{change the argument to be the free space in the destination buffer minus the terminating null byte}}
anatofuz
parents:
diff changeset
71 }
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 // Non-array type gets a different error message.
anatofuz
parents:
diff changeset
74 void f(char* s, char* d) {
anatofuz
parents:
diff changeset
75 strncat(d, s, sizeof(d)); // expected-warning {{the value of the size argument to 'strncat' is wrong}}
anatofuz
parents:
diff changeset
76 }