Debian Codegraph @

gcc/testsuite/gcc.dg/Wstringop-overflow-39.c

raw
1 /* PR middle-end/95667 - unintended warning for memset writing across multiple
2 members
3 { dg-do compile }
4 { dg-options "-O2 -Wall" } */
5
6 extern void sink (void*);
7
8 struct S1 { char a[3], b[5]; };
9
10 void warn_strcpy_s1 (void)
11 {
12 struct S1 *p = __builtin_malloc (sizeof *p);
13 char s[] = "1234567";
14 __builtin_strcpy (p->a, s); // { dg-warning "\\\[-Wstringop-overflow" }
15 sink (p);
16 }
17
18 void nowarn_memset_s1 (void)
19 {
20 struct S1 *p = __builtin_malloc (sizeof *p);
21 __builtin_memset (p->a, 0, 8); // { dg-bogus "\\\[-Wstringop-overflow" }
22 sink (p);
23 }
24
25 struct S2 { char a[2], b[2][2], c[3]; };
26
27 void nowarn_memset_s2 (void)
28 {
29 struct S2 *p = __builtin_malloc (sizeof *p);
30
31 __builtin_memset (p->a, 0, sizeof *p);
32 sink (p);
33
34 __builtin_memset (p->b, 0, 7);
35 sink (p);
36
37 __builtin_memset (&p->b[0], 0, 7);
38 sink (p);
39
40 __builtin_memset (&p->b[1], 0, 5);
41 sink (p);
42
43 __builtin_memset (&p->b[0][0], 0, 7);
44 sink (p);
45
46 __builtin_memset (&p->b[0][1], 0, 6);
47 sink (p);
48
49 __builtin_memset (&p->b[1][0], 0, 5);
50 sink (p);
51
52 __builtin_memset (&p->b[1][1], 0, 4);
53 sink (p);
54 }
55
56 void warn_memset_s2 (void)
57 {
58 const unsigned n = sizeof (struct S2);
59 struct S2 *p = __builtin_malloc (n);
60
61 /* These should trigger -Wstringop-overflow rather than -Warray-bounds
62 but the main purpose of the test is to verify the absence of warnings
63 above so the exact warning for these overflwing calls isn't important
64 here. */
65
66 __builtin_memset (p->a, 0, n + 1); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
67 sink (p);
68
69 __builtin_memset (p->b, 0, 8); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
70 sink (p);
71
72 __builtin_memset (&p->b[0], 0, 8); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
73 sink (p);
74
75 __builtin_memset (&p->b[0][0], 0, 8); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
76 sink (p);
77
78 __builtin_memset (&p->b[1], 0, 6); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
79 sink (p);
80
81 __builtin_memset (&p->b[0][1], 0, 7); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
82 sink (p);
83 }
84
85 void nowarn_vl_struct (unsigned n)
86 {
87 if (n < 3 || 5 < n)
88 n = 3;
89
90 struct V { char a[3], b[n], c[7]; } v;
91
92 __builtin_memset (v.a, 0, 15);
93 sink (&v);
94
95 __builtin_memset (v.b, 0, 12);
96 sink (&v);
97
98 __builtin_memset (v.c, 0, 7);
99 sink (&v);
100
101 __builtin_memset (v.a, 0, 16); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
102 sink (&v);
103
104 __builtin_memset (v.b, 0, 13); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" }
105 sink (&v);
106
107 /* The &V.C argument is represented as a variable offset from
108 the beginning of the allocated object and there's no good
109 way to figure out from its variable offset that it's base
110 is the C member:
111 s.1_12 = __builtin_alloca_with_align (prephitmp_24, 8);
112 _9 = s.1_12 + prephitmp_27;
113 __builtin_memset (_9, 0, 2);
114 */
115
116 __builtin_memset (v.c, 0, 8); // { dg-warning "\\\[-Warray-bounds|-Wstringop-overflow" "pr?????" { xfail *-*-* } }
117 sink (&v);
118 }