Debian Codegraph @

gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess1.c

raw
1 /* Test -Wsizeof-pointer-memaccess warnings. */
2 /* { dg-do compile } */
3 /* { dg-options "-Wall -Wno-array-bounds -Wno-sizeof-array-argument -Wno-stringop-overflow -Wno-stringop-overread" } */
4 /* { dg-options "-Wall -Wno-array-bounds -Wno-sizeof-array-argument -Wno-c++-compat -Wno-stringop-overflow -Wno-stringop-overread" { target c } } */
5
6 typedef __SIZE_TYPE__ size_t;
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 extern int snprintf (char *, size_t, const char *, ...);
11 extern int vsnprintf (char *, size_t, const char *, __builtin_va_list);
12 extern void *memchr (const void *, int, size_t);
13 #ifdef __cplusplus
14 }
15 #endif
16
17 struct A { short a, b; int c, d; long e, f; };
18 typedef struct A TA;
19 typedef struct A *PA;
20 typedef TA *PTA;
21 struct B {};
22 typedef struct B TB;
23 typedef struct B *PB;
24 typedef TB *PTB;
25 typedef int X[3][3][3];
26
27 void foo (void **);
28
29 void
30 f1 (void *x)
31 {
32 struct A a, *pa1 = &a;
33 TA *pa2 = &a;
34 PA pa3 = &a;
35 PTA pa4 = &a;
36 void *arr[100];
37 int i = 0;
38 arr[i++] = memchr (&a, 0, sizeof (&a)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
39 arr[i++] = memchr (pa1, 0, sizeof (pa1)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
40 arr[i++] = memchr (pa2, 0, sizeof pa2); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
41 arr[i++] = memchr (pa3, 0, sizeof (pa3)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
42 arr[i++] = memchr (pa4, 0, sizeof pa4); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
43 arr[i++] = memchr (pa1, 0, sizeof (struct A *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
44 arr[i++] = memchr (pa2, 0, sizeof (PTA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
45 arr[i++] = memchr (pa3, 0, sizeof (PA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
46 arr[i++] = memchr (pa4, 0, sizeof (__typeof (pa4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
47
48 /* These are correct, no warning. */
49 arr[i++] = memchr (&a, 0, sizeof a);
50 arr[i++] = memchr (&a, 0, sizeof (a));
51 arr[i++] = memchr (&a, 0, sizeof (struct A));
52 arr[i++] = memchr (&a, 0, sizeof (const struct A));
53 arr[i++] = memchr (&a, 0, sizeof (volatile struct A));
54 arr[i++] = memchr (&a, 0, sizeof (volatile const struct A));
55 arr[i++] = memchr (&a, 0, sizeof (TA));
56 arr[i++] = memchr (&a, 0, sizeof (__typeof (*&a)));
57 arr[i++] = memchr (pa1, 0, sizeof (*pa1));
58 arr[i++] = memchr (pa2, 0, sizeof (*pa3));
59 arr[i++] = memchr (pa3, 0, sizeof (__typeof (*pa3)));
60 /* These are probably broken, but obfuscated, no warning. */
61 arr[i++] = memchr ((void *) &a, 0, sizeof (&a));
62 arr[i++] = memchr ((char *) &a, 0, sizeof (&a));
63 arr[i++] = memchr (&a, 0, sizeof (&a) + 0);
64 arr[i++] = memchr (&a, 0, 0 + sizeof (&a));
65
66 foo (arr);
67 }
68
69 void
70 f2 (void *x)
71 {
72 struct B b, *pb1 = &b;
73 TB *pb2 = &b;
74 PB pb3 = &b;
75 PTB pb4 = &b;
76 void *arr[100];
77 int i = 0;
78 arr[i++] = memchr (&b, 0, sizeof (&b)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
79 arr[i++] = memchr (pb1, 0, sizeof (pb1)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
80 arr[i++] = memchr (pb2, 0, sizeof pb2); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
81 arr[i++] = memchr (pb3, 0, sizeof (pb3)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
82 arr[i++] = memchr (pb4, 0, sizeof pb4); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
83 arr[i++] = memchr (pb1, 0, sizeof (struct B *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
84 arr[i++] = memchr (pb2, 0, sizeof (PTB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
85 arr[i++] = memchr (pb3, 0, sizeof (PB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
86 arr[i++] = memchr (pb4, 0, sizeof (__typeof (pb4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
87
88 /* These are correct, no warning. */
89 arr[i++] = memchr (&b, 0, sizeof b);
90 arr[i++] = memchr (&b, 0, sizeof (b));
91 arr[i++] = memchr (&b, 0, sizeof (struct B));
92 arr[i++] = memchr (&b, 0, sizeof (const struct B));
93 arr[i++] = memchr (&b, 0, sizeof (volatile struct B));
94 arr[i++] = memchr (&b, 0, sizeof (volatile const struct B));
95 arr[i++] = memchr (&b, 0, sizeof (TB));
96 arr[i++] = memchr (&b, 0, sizeof (__typeof (*&b)));
97 arr[i++] = memchr (pb1, 0, sizeof (*pb1));
98 arr[i++] = memchr (pb2, 0, sizeof (*pb3));
99 arr[i++] = memchr (pb3, 0, sizeof (__typeof (*pb3)));
100 /* These are probably broken, but obfuscated, no warning. */
101 arr[i++] = memchr ((void *) &b, 0, sizeof (&b));
102 arr[i++] = memchr ((char *) &b, 0, sizeof (&b));
103 arr[i++] = memchr (&b, 0, sizeof (&b) + 0);
104 arr[i++] = memchr (&b, 0, 0 + sizeof (&b));
105
106 foo (arr);
107 }
108
109 void
110 f3 (void *x, char *y, int z, X w)
111 {
112 unsigned char *y1 = (unsigned char *) __builtin_alloca (z + 16);
113 char buf1[7];
114 signed char buf2[z + 32];
115 long buf3[17];
116 int *buf4[9];
117 signed char *y2 = buf2;
118 char c;
119 void *arr[100];
120 int i = 0;
121 arr[i++] = memchr (y, 0, sizeof (y)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
122 arr[i++] = memchr (y1, 0, sizeof (y1)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
123 arr[i++] = memchr (y2, 0, sizeof (y2)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
124 arr[i++] = memchr (&c, 0, sizeof (&c)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
125 arr[i++] = memchr (w, 0, sizeof w); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
126
127 /* These are correct, no warning. */
128 arr[i++] = memchr (y, 0, sizeof (*y));
129 arr[i++] = memchr (y1, 0, sizeof (*y2));
130 arr[i++] = memchr (buf1, 0, sizeof buf1);
131 arr[i++] = memchr (buf3, 0, sizeof (buf3));
132 arr[i++] = memchr (&buf3[0], 0, sizeof (buf3));
133 arr[i++] = memchr (&buf4[0], 0, sizeof (buf4));
134 arr[i++] = memchr (w, 0, sizeof (X));
135 /* These are probably broken, but obfuscated, no warning. */
136 arr[i++] = memchr ((void *) y, 0, sizeof (y));
137 arr[i++] = memchr ((char *) y1, 0, sizeof (y2));
138 arr[i++] = memchr (y, 0, sizeof (y) + 0);
139 arr[i++] = memchr (y1, 0, 0 + sizeof (y2));
140 arr[i++] = memchr ((void *) &c, 0, sizeof (&c));
141 arr[i++] = memchr ((signed char *) &c, 0, sizeof (&c));
142 arr[i++] = memchr (&c, 0, sizeof (&c) + 0);
143 arr[i++] = memchr (&c, 0, 0 + sizeof (&c));
144
145 foo (arr);
146 }
147
148 void
149 f4 (char x[64], char *y, __builtin_va_list ap)
150 {
151 char buf[128], *p = buf;
152 snprintf (x, sizeof (x), "%s", y); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
153 vsnprintf (x, sizeof (x), "%s", ap); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
154 snprintf (p, sizeof (p), "%s", y); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
155 vsnprintf (p, sizeof (p), "%s", ap); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
156
157 /* These are correct, no warning. */
158 snprintf (buf, sizeof (buf), "%s", y);
159 vsnprintf (buf, sizeof (buf), "%s", ap);
160 snprintf (p, sizeof (buf), "%s", y);
161 vsnprintf (p, sizeof (buf), "%s", ap);
162 }
163
164 /* { dg-prune-output "-Wuninitialized" } */