libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / Wstringop-overflow-66.c
blob0ecf51149e56fc3b1ca51aa7dec4558b6e57fd85
1 /* PR middle-end/97631 - bogus "writing one too many bytes" warning for
2 memcpy with strlen argument
3 { dg-do compile }
4 { dg-options "-O2 -Wall" } */
6 #define NOIPA __attribute__ ((noipa))
8 typedef __SIZE_TYPE__ size_t;
10 extern void* malloc (size_t);
11 extern void* memcpy (void*, const void*, size_t);
12 extern void* memmove (void*, const void*, size_t);
13 extern void* memset (void*, int, size_t);
14 extern char* strcpy (char*, const char*);
15 extern char* strncpy (char*, const char*, size_t);
16 extern size_t strlen (const char*);
19 NOIPA char* nowarn_strcpy (char *s)
21 size_t n = strlen (s);
22 char *d = malloc (n + 1);
23 strcpy (d, s);
24 return d;
28 NOIPA char* warn_strcpy (char *s)
30 size_t n = strlen (s);
31 char *d = malloc (n);
32 strcpy (d, s); // { dg-warning "\\\[-Wstringop-overflow" }
33 return d;
36 NOIPA char* warn_strcpy_nz (char *s)
38 size_t n = strlen (s);
39 if (n == 0)
40 return 0;
42 char *d = malloc (n);
43 strcpy (d, s); // { dg-warning "\\\[-Wstringop-overflow" }
44 return d;
47 NOIPA char* warn_strcpy_nn (char *s)
49 size_t n = strlen (s);
50 char *d = malloc (n);
51 if (!d)
52 return 0;
54 strcpy (d, s); // { dg-warning "\\\[-Wstringop-overflow" }
55 return d;
58 NOIPA char* warn_strcpy_nz_nn (char *s)
60 size_t n = strlen (s);
61 if (n == 0)
62 return 0;
64 char *d = malloc (n);
65 if (!d)
66 return 0;
68 strcpy (d, s); // { dg-warning "\\\[-Wstringop-overflow" }
69 return d;
73 NOIPA char* nowarn_strncpy_1 (char *s)
75 /* There's no overflow or truncation below so verify there is no
76 warning either. */
77 size_t n = strlen (s) + 1;
78 char *d = malloc (n);
79 strncpy (d, s, n);
80 return d;
84 NOIPA char* warn_strncpy (char *s)
86 size_t n = strlen (s);
87 char *d = malloc (n);
88 strncpy (d, s, n); // { dg-warning "\\\[-Wstringop-truncation" }
89 return d;
92 NOIPA char* warn_strncpy_p1 (char *s)
94 size_t n = strlen (s);
95 char *d = malloc (n + 1);
96 strncpy (d, s, n); // { dg-warning "\\\[-Wstringop-truncation" }
97 return d;
100 NOIPA char* warn_strncpy_nz (char *s)
102 size_t n = strlen (s);
103 if (n == 0)
104 return 0;
106 char *d = malloc (n);
107 strncpy (d, s, n); // { dg-warning "\\\[-Wstringop-truncation" }
108 return d;
113 NOIPA char* nowarn_memcpy (char *s)
115 size_t n = strlen (s);
116 char *d = malloc (n);
117 memcpy (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
118 return d;
121 NOIPA char* nowarn_memcpy_nz (char *s)
123 size_t n = strlen (s);
124 if (n == 0)
125 return 0;
127 char *d = malloc (n);
128 memcpy (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
129 return d;
132 NOIPA char* nowarn_memcpy_nn (char *s)
134 size_t n = strlen (s);
135 char *d = malloc (n);
136 if (!d)
137 return 0;
139 memcpy (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
140 return d;
143 NOIPA char* nowarn_memcpy_nn_nz (char *s)
145 size_t n = strlen (s);
146 if (n == 0)
147 return 0;
149 char *d = malloc (n);
150 if (!d)
151 return 0;
153 memcpy (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
154 return d;
159 NOIPA char* nowarn_memmove (char *s)
161 size_t n = strlen (s);
162 if (n == 0)
163 return 0;
165 char *d = malloc (n);
166 memmove (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
167 return d;
171 NOIPA char* nowarn_memset (char *s, int c)
173 size_t n = strlen (s);
174 if (n == 0)
175 return 0;
177 char *d = malloc (n);
178 memset (d, c, n); // { dg-bogus "\\\[-Wstringop-overflow" }
179 return d;