1 /* PR middle-end/97631 - bogus "writing one too many bytes" warning for
2 memcpy with strlen argument
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);
28 NOIPA
char* warn_strcpy (char *s
)
30 size_t n
= strlen (s
);
32 strcpy (d
, s
); // { dg-warning "\\\[-Wstringop-overflow" }
36 NOIPA
char* warn_strcpy_nz (char *s
)
38 size_t n
= strlen (s
);
43 strcpy (d
, s
); // { dg-warning "\\\[-Wstringop-overflow" }
47 NOIPA
char* warn_strcpy_nn (char *s
)
49 size_t n
= strlen (s
);
54 strcpy (d
, s
); // { dg-warning "\\\[-Wstringop-overflow" }
58 NOIPA
char* warn_strcpy_nz_nn (char *s
)
60 size_t n
= strlen (s
);
68 strcpy (d
, s
); // { dg-warning "\\\[-Wstringop-overflow" }
73 NOIPA
char* nowarn_strncpy_1 (char *s
)
75 /* There's no overflow or truncation below so verify there is no
77 size_t n
= strlen (s
) + 1;
84 NOIPA
char* warn_strncpy (char *s
)
86 size_t n
= strlen (s
);
88 strncpy (d
, s
, n
); // { dg-warning "\\\[-Wstringop-truncation" }
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" }
100 NOIPA
char* warn_strncpy_nz (char *s
)
102 size_t n
= strlen (s
);
106 char *d
= malloc (n
);
107 strncpy (d
, s
, n
); // { dg-warning "\\\[-Wstringop-truncation" }
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" }
121 NOIPA
char* nowarn_memcpy_nz (char *s
)
123 size_t n
= strlen (s
);
127 char *d
= malloc (n
);
128 memcpy (d
, s
, n
); // { dg-bogus "\\\[-Wstringop-overflow" }
132 NOIPA
char* nowarn_memcpy_nn (char *s
)
134 size_t n
= strlen (s
);
135 char *d
= malloc (n
);
139 memcpy (d
, s
, n
); // { dg-bogus "\\\[-Wstringop-overflow" }
143 NOIPA
char* nowarn_memcpy_nn_nz (char *s
)
145 size_t n
= strlen (s
);
149 char *d
= malloc (n
);
153 memcpy (d
, s
, n
); // { dg-bogus "\\\[-Wstringop-overflow" }
159 NOIPA
char* nowarn_memmove (char *s
)
161 size_t n
= strlen (s
);
165 char *d
= malloc (n
);
166 memmove (d
, s
, n
); // { dg-bogus "\\\[-Wstringop-overflow" }
171 NOIPA
char* nowarn_memset (char *s
, int c
)
173 size_t n
= strlen (s
);
177 char *d
= malloc (n
);
178 memset (d
, c
, n
); // { dg-bogus "\\\[-Wstringop-overflow" }