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" } */
5
6 #define NOIPA __attribute__ ((noipa))
7
8 typedef __SIZE_TYPE__ size_t;
9
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*);
17
18
nowarn_strcpy(char * s)19 NOIPA char* nowarn_strcpy (char *s)
20 {
21 size_t n = strlen (s);
22 char *d = malloc (n + 1);
23 strcpy (d, s);
24 return d;
25 }
26
27
warn_strcpy(char * s)28 NOIPA char* warn_strcpy (char *s)
29 {
30 size_t n = strlen (s);
31 char *d = malloc (n);
32 strcpy (d, s); // { dg-warning "\\\[-Wstringop-overflow" }
33 return d;
34 }
35
warn_strcpy_nz(char * s)36 NOIPA char* warn_strcpy_nz (char *s)
37 {
38 size_t n = strlen (s);
39 if (n == 0)
40 return 0;
41
42 char *d = malloc (n);
43 strcpy (d, s); // { dg-warning "\\\[-Wstringop-overflow" }
44 return d;
45 }
46
warn_strcpy_nn(char * s)47 NOIPA char* warn_strcpy_nn (char *s)
48 {
49 size_t n = strlen (s);
50 char *d = malloc (n);
51 if (!d)
52 return 0;
53
54 strcpy (d, s); // { dg-warning "\\\[-Wstringop-overflow" }
55 return d;
56 }
57
warn_strcpy_nz_nn(char * s)58 NOIPA char* warn_strcpy_nz_nn (char *s)
59 {
60 size_t n = strlen (s);
61 if (n == 0)
62 return 0;
63
64 char *d = malloc (n);
65 if (!d)
66 return 0;
67
68 strcpy (d, s); // { dg-warning "\\\[-Wstringop-overflow" }
69 return d;
70 }
71
72
nowarn_strncpy_1(char * s)73 NOIPA char* nowarn_strncpy_1 (char *s)
74 {
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;
81 }
82
83
warn_strncpy(char * s)84 NOIPA char* warn_strncpy (char *s)
85 {
86 size_t n = strlen (s);
87 char *d = malloc (n);
88 strncpy (d, s, n); // { dg-warning "\\\[-Wstringop-truncation" }
89 return d;
90 }
91
warn_strncpy_p1(char * s)92 NOIPA char* warn_strncpy_p1 (char *s)
93 {
94 size_t n = strlen (s);
95 char *d = malloc (n + 1);
96 strncpy (d, s, n); // { dg-warning "\\\[-Wstringop-truncation" }
97 return d;
98 }
99
warn_strncpy_nz(char * s)100 NOIPA char* warn_strncpy_nz (char *s)
101 {
102 size_t n = strlen (s);
103 if (n == 0)
104 return 0;
105
106 char *d = malloc (n);
107 strncpy (d, s, n); // { dg-warning "\\\[-Wstringop-truncation" }
108 return d;
109
110 }
111
112
nowarn_memcpy(char * s)113 NOIPA char* nowarn_memcpy (char *s)
114 {
115 size_t n = strlen (s);
116 char *d = malloc (n);
117 memcpy (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
118 return d;
119 }
120
nowarn_memcpy_nz(char * s)121 NOIPA char* nowarn_memcpy_nz (char *s)
122 {
123 size_t n = strlen (s);
124 if (n == 0)
125 return 0;
126
127 char *d = malloc (n);
128 memcpy (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
129 return d;
130 }
131
nowarn_memcpy_nn(char * s)132 NOIPA char* nowarn_memcpy_nn (char *s)
133 {
134 size_t n = strlen (s);
135 char *d = malloc (n);
136 if (!d)
137 return 0;
138
139 memcpy (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
140 return d;
141 }
142
nowarn_memcpy_nn_nz(char * s)143 NOIPA char* nowarn_memcpy_nn_nz (char *s)
144 {
145 size_t n = strlen (s);
146 if (n == 0)
147 return 0;
148
149 char *d = malloc (n);
150 if (!d)
151 return 0;
152
153 memcpy (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
154 return d;
155
156 }
157
158
nowarn_memmove(char * s)159 NOIPA char* nowarn_memmove (char *s)
160 {
161 size_t n = strlen (s);
162 if (n == 0)
163 return 0;
164
165 char *d = malloc (n);
166 memmove (d, s, n); // { dg-bogus "\\\[-Wstringop-overflow" }
167 return d;
168 }
169
170
nowarn_memset(char * s,int c)171 NOIPA char* nowarn_memset (char *s, int c)
172 {
173 size_t n = strlen (s);
174 if (n == 0)
175 return 0;
176
177 char *d = malloc (n);
178 memset (d, c, n); // { dg-bogus "\\\[-Wstringop-overflow" }
179 return d;
180 }
181