2 /* { dg-options "-O2 -Wno-stringop-overread" } */
3 /* { dg-require-effective-target alloca } */
4 /* { dg-additional-options "-DSKIP_STRNDUP" { target { ! strndup } } } */
6 #include "builtin-object-size-common.h"
17 extern struct A extc
[];
21 __attribute__ ((noinline
))
22 test1 (void *q
, int x
)
25 void *p
= &a
.a
[3], *r
;
27 struct A vara
[x
+ 10];
32 if (__builtin_object_size (p
, 1) != sizeof (a
.a
) - 3)
34 if (__builtin_object_size (&a
.c
[9], 1)
37 if (__builtin_object_size (q
, 1) != (size_t) -1)
39 #ifdef __builtin_object_size
41 ? __builtin_object_size (r
, 1) != sizeof (a
.a
) - 9
42 : __builtin_object_size (r
, 1) != sizeof (a
.c
) - 1)
45 if (__builtin_object_size (r
, 1) != sizeof (a
.c
) - 1)
52 if (__builtin_object_size (&y
, 1) != sizeof (y
))
54 if (__builtin_object_size (w
, 1) != sizeof (w
))
56 if (__builtin_object_size (&y
.b
, 1) != sizeof (a
.b
))
58 #ifdef __builtin_object_size
60 ? __builtin_object_size (r
, 1) != sizeof (a
.a
) - 1
61 : __builtin_object_size (r
, 1) != sizeof (a
.a
) - 6)
64 if (__builtin_object_size (r
, 1) != sizeof (a
.a
) - 1)
71 #ifdef __builtin_object_size
72 if (__builtin_object_size (r
, 1) != (x
< 20 ? 30 : 2 * 16))
75 /* We may duplicate this test onto the two exit paths. On one path
76 the size will be 32, the other it will be 30. If we don't duplicate
77 this test, then the size will be 32. */
78 if (__builtin_object_size (r
, 1) != 2 * 16
79 && __builtin_object_size (r
, 1) != 30)
86 #ifdef __builtin_object_size
87 if (__builtin_object_size (r
, 1) != (x
< 20 ? 30 : 2 * 14))
90 if (__builtin_object_size (r
, 1) != 30)
94 r
= malloc (sizeof (a
));
97 #ifdef __builtin_object_size
98 if (__builtin_object_size (r
, 1) != (x
< 30 ? sizeof (a
) : sizeof (a
) - 3))
101 if (__builtin_object_size (r
, 1) != sizeof (a
))
104 r
= memcpy (r
, "a", 2);
105 #ifdef __builtin_object_size
106 if (__builtin_object_size (r
, 1) != (x
< 30 ? sizeof (a
) : sizeof (a
) - 3))
109 if (__builtin_object_size (r
, 1) != sizeof (a
))
112 r
= memcpy (r
+ 2, "b", 2) + 2;
113 #ifdef __builtin_object_size
114 if (__builtin_object_size (r
, 0)
115 != (x
< 30 ? sizeof (a
) - 4 : sizeof (a
) - 7))
118 if (__builtin_object_size (r
, 1) != sizeof (a
) - 4)
122 r
= memset (r
, 'a', 2);
123 if (__builtin_object_size (r
, 1) != sizeof (a
.a
) - 4)
125 r
= memset (r
+ 2, 'b', 2) + 2;
126 if (__builtin_object_size (r
, 1) != sizeof (a
.a
) - 8)
129 r
= strcpy (r
, "ab");
130 if (__builtin_object_size (r
, 1) != sizeof (a
.a
) - 1)
132 r
= strcpy (r
+ 2, "cd") + 2;
133 if (__builtin_object_size (r
, 1) != sizeof (a
.a
) - 5)
135 if (__builtin_object_size (exta
, 1) != (size_t) -1)
137 if (__builtin_object_size (exta
+ 10, 1) != (size_t) -1)
139 if (__builtin_object_size (&exta
[5], 1) != (size_t) -1)
141 if (__builtin_object_size (extb
, 1) != sizeof (extb
))
143 if (__builtin_object_size (extb
+ 10, 1) != sizeof (extb
) - 10)
145 if (__builtin_object_size (&extb
[5], 1) != sizeof (extb
) - 5)
147 if (__builtin_object_size (extc
, 1) != (size_t) -1)
149 if (__builtin_object_size (extc
+ 10, 1) != (size_t) -1)
151 if (__builtin_object_size (&extc
[5], 1) != (size_t) -1)
153 if (__builtin_object_size (&extc
->a
, 1) != (size_t) -1)
155 if (__builtin_object_size (&(extc
+ 10)->b
, 1) != (size_t) -1)
157 if (__builtin_object_size (&extc
[5].c
[3], 1) != (size_t) -1)
159 #ifdef __builtin_object_size
160 if (__builtin_object_size (var
, 1) != x
+ 10)
162 if (__builtin_object_size (var
+ 10, 1) != x
)
164 if (__builtin_object_size (&var
[5], 1) != x
+ 5)
166 if (__builtin_object_size (vara
, 1) != (x
+ 10) * sizeof (struct A
))
168 if (__builtin_object_size (vara
+ 10, 1) != x
* sizeof (struct A
))
170 if (__builtin_object_size (&vara
[5], 1) != (x
+ 5) * sizeof (struct A
))
173 if (__builtin_object_size (var
, 1) != (size_t) -1)
175 if (__builtin_object_size (var
+ 10, 1) != (size_t) -1)
177 if (__builtin_object_size (&var
[5], 1) != (size_t) -1)
179 if (__builtin_object_size (vara
, 1) != (size_t) -1)
181 if (__builtin_object_size (vara
+ 10, 1) != (size_t) -1)
183 if (__builtin_object_size (&vara
[5], 1) != (size_t) -1)
186 if (__builtin_object_size (&vara
[0].a
, 1) != sizeof (vara
[0].a
))
188 if (__builtin_object_size (&vara
[10].a
[0], 1) != sizeof (vara
[0].a
))
190 if (__builtin_object_size (&vara
[5].a
[4], 1) != sizeof (vara
[0].a
) - 4)
192 if (__builtin_object_size (&vara
[5].b
, 1) != sizeof (vara
[0].b
))
194 if (__builtin_object_size (&vara
[7].c
[7], 1) != sizeof (vara
[0].c
) - 7)
196 if (__builtin_object_size (zerol
, 1) != 0)
198 if (__builtin_object_size (&zerol
, 1) != 0)
200 if (__builtin_object_size (&zerol
[0], 1) != 0)
202 if (__builtin_object_size (zerol
[0].a
, 1) != 0)
204 if (__builtin_object_size (&zerol
[0].a
[0], 1) != 0)
206 if (__builtin_object_size (&zerol
[0].b
, 1) != 0)
208 if (__builtin_object_size ("abcdefg", 1) != sizeof ("abcdefg"))
210 if (__builtin_object_size ("abcd\0efg", 1) != sizeof ("abcd\0efg"))
212 if (__builtin_object_size (&"abcd\0efg", 1) != sizeof ("abcd\0efg"))
214 if (__builtin_object_size (&"abcd\0efg"[0], 1) != sizeof ("abcd\0efg"))
216 if (__builtin_object_size (&"abcd\0efg"[4], 1) != sizeof ("abcd\0efg") - 4)
218 if (__builtin_object_size ("abcd\0efg" + 5, 1) != sizeof ("abcd\0efg") - 5)
220 if (__builtin_object_size (L
"abcdefg", 1) != sizeof (L
"abcdefg"))
222 r
= (char *) L
"abcd\0efg";
223 if (__builtin_object_size (r
+ 2, 1) != sizeof (L
"abcd\0efg") - 2)
230 __attribute__ ((noinline
))
233 struct B
{ char buf1
[10]; char buf2
[10]; } a
;
236 #ifdef __builtin_object_size
240 if (sizeof (a
) != 20)
244 for (i
= 0; i
< 4; ++i
)
250 else if (i
== l1
+ 1)
252 else if (i
== l1
+ 2)
255 #ifdef __builtin_object_size
256 dyn_res
= sizeof (buf3
);
258 for (i
= 0; i
< 4; ++i
)
261 dyn_res
= sizeof (a
.buf1
) - 1;
263 dyn_res
= sizeof (a
.buf2
) - 7;
264 else if (i
== l1
+ 1)
265 dyn_res
= sizeof (buf3
) - 5;
266 else if (i
== l1
+ 2)
267 dyn_res
= sizeof (a
.buf1
) - 9;
269 if (__builtin_object_size (r
, 1) != dyn_res
)
272 if (__builtin_object_size (r
, 1) != sizeof (buf3
))
276 for (i
= 0; i
< 4; ++i
)
282 else if (i
== l1
+ 1)
284 else if (i
== l1
+ 2)
287 #ifdef __builtin_object_size
288 dyn_res
= sizeof (buf3
) - 20;
290 for (i
= 0; i
< 4; ++i
)
293 dyn_res
= sizeof (a
.buf1
) - 7;
295 dyn_res
= sizeof (a
.buf2
) - 7;
296 else if (i
== l1
+ 1)
297 dyn_res
= sizeof (buf3
) - 5;
298 else if (i
== l1
+ 2)
299 dyn_res
= sizeof (a
.buf1
) - 9;
301 if (__builtin_object_size (r
, 1) != dyn_res
)
304 if (__builtin_object_size (r
, 1) != sizeof (buf3
) - 5)
308 #ifdef __builtin_object_size
312 if (__builtin_object_size (r
, 1) != dyn_res
)
317 if (__builtin_object_size (r
+ 6, 1) != dyn_res
- 6)
320 else if (__builtin_object_size (r
+ 6, 1) != 0)
323 else if (__builtin_object_size (r
, 1) != 0)
326 if (__builtin_object_size (r
, 1) != sizeof (buf3
) - 13)
328 if (__builtin_object_size (r
+ 6, 1) != sizeof (buf3
) - 19)
334 __attribute__ ((noinline
))
338 struct B
{ struct A a
[2]; struct A b
; char c
[4]; char d
; double e
;
339 _Complex
double f
; } x
;
344 if (__builtin_object_size (buf4
, 1) != sizeof (buf4
))
346 if (__builtin_object_size (&buf4
, 1) != sizeof (buf4
))
348 if (__builtin_object_size (&buf4
[0], 1) != sizeof (buf4
))
350 if (__builtin_object_size (&buf4
[1], 1) != sizeof (buf4
) - 1)
352 if (__builtin_object_size (&x
, 1) != sizeof (x
))
354 if (__builtin_object_size (&x
.a
, 1) != sizeof (x
.a
))
356 if (__builtin_object_size (&x
.a
[0], 1) != sizeof (x
.a
))
358 if (__builtin_object_size (&x
.a
[0].a
, 1) != sizeof (x
.a
[0].a
))
360 if (__builtin_object_size (&x
.a
[0].a
[0], 1) != sizeof (x
.a
[0].a
))
362 if (__builtin_object_size (&x
.a
[0].a
[3], 1) != sizeof (x
.a
[0].a
) - 3)
364 if (__builtin_object_size (&x
.a
[0].b
, 1) != sizeof (x
.a
[0].b
))
366 if (__builtin_object_size (&x
.a
[1].c
, 1) != sizeof (x
.a
[1].c
))
368 if (__builtin_object_size (&x
.a
[1].c
[0], 1) != sizeof (x
.a
[1].c
))
370 if (__builtin_object_size (&x
.a
[1].c
[3], 1) != sizeof (x
.a
[1].c
) - 3)
372 if (__builtin_object_size (&x
.b
, 1) != sizeof (x
.b
))
374 if (__builtin_object_size (&x
.b
.a
, 1) != sizeof (x
.b
.a
))
376 if (__builtin_object_size (&x
.b
.a
[0], 1) != sizeof (x
.b
.a
))
378 if (__builtin_object_size (&x
.b
.a
[3], 1) != sizeof (x
.b
.a
) - 3)
380 if (__builtin_object_size (&x
.b
.b
, 1) != sizeof (x
.b
.b
))
382 if (__builtin_object_size (&x
.b
.c
, 1) != sizeof (x
.b
.c
))
384 if (__builtin_object_size (&x
.b
.c
[0], 1) != sizeof (x
.b
.c
))
386 if (__builtin_object_size (&x
.b
.c
[3], 1) != sizeof (x
.b
.c
) - 3)
388 if (__builtin_object_size (&x
.c
, 1) != sizeof (x
.c
))
390 if (__builtin_object_size (&x
.c
[0], 1) != sizeof (x
.c
))
392 if (__builtin_object_size (&x
.c
[1], 1) != sizeof (x
.c
) - 1)
394 if (__builtin_object_size (&x
.d
, 1) != sizeof (x
.d
))
396 if (__builtin_object_size (&x
.e
, 1) != sizeof (x
.e
))
398 if (__builtin_object_size (&x
.f
, 1) != sizeof (x
.f
))
401 if (__builtin_object_size (dp
, 1) != sizeof (x
.f
) / 2)
404 if (__builtin_object_size (dp
, 1) != sizeof (x
.f
) / 2)
407 if (__builtin_object_size (dp
, 1) != sizeof (y
))
409 if (__builtin_object_size (&z
, 1) != sizeof (z
))
412 if (__builtin_object_size (dp
, 1) != sizeof (z
) / 2)
415 if (__builtin_object_size (dp
, 1) != sizeof (z
) / 2)
419 struct S
{ unsigned int a
; };
422 __attribute__ ((noinline
))
423 test4 (char *x
, int y
)
428 for (i
= 0; i
< y
; i
++)
432 if (__builtin_object_size (p
, 1) != (size_t) -1)
439 __attribute__ ((noinline
))
442 struct T
{ char buf
[64]; char buf2
[64]; } t
;
446 for (i
= 0; i
< x
; ++i
)
448 #ifdef __builtin_object_size
449 if (__builtin_object_size (p
, 1) != sizeof (t
.buf
) - 8 - 4 * x
)
452 if (__builtin_object_size (p
, 1) != sizeof (t
.buf
) - 8)
455 memset (p
, ' ', sizeof (t
.buf
) - 8 - 4 * 4);
459 __attribute__ ((noinline
))
463 struct T
{ char buf
[64]; char buf2
[64]; } t
;
464 char *p
= &buf
[64], *q
= &t
.buf
[64];
466 if (__builtin_object_size (p
+ 64, 1) != 0)
468 if (__builtin_object_size (q
+ 0, 1) != 0)
470 if (__builtin_object_size (q
+ 64, 1) != 0)
475 __attribute__ ((noinline
))
478 struct T
{ char buf
[10]; char buf2
[10]; } t
;
479 char *p
= &t
.buf2
[-4];
480 char *q
= &t
.buf2
[0];
481 if (__builtin_object_size (p
, 1) != 0)
483 if (__builtin_object_size (q
, 1) != sizeof (t
.buf2
))
486 if (__builtin_object_size (q
, 1) != 0)
489 if (__builtin_object_size (q
, 1) != 0)
492 if (__builtin_object_size (p
, 1) != 0)
497 __attribute__ ((noinline
))
498 test8 (unsigned cond
)
500 char *buf2
= malloc (10);
508 #ifdef __builtin_object_size
509 if (__builtin_object_size (&p
[-4], 1) != (cond
? 6 : 10))
512 if (__builtin_object_size (&p
[-4], 1) != 10)
516 for (unsigned i
= cond
; i
> 0; i
--)
519 #ifdef __builtin_object_size
520 if (__builtin_object_size (p
, 1) != ((cond
? 2 : 6) + cond
))
523 if (__builtin_object_size (p
, 1) != 10)
528 for (unsigned i
= cond
; i
> 0; i
--)
531 #ifdef __builtin_object_size
532 if (__builtin_object_size (p
, 1) != sizeof (y
.c
) - 8 + cond
)
535 if (__builtin_object_size (p
, 1) != sizeof (y
.c
))
541 /* Tests for strdup/strndup. */
543 __attribute__ ((noinline
))
546 const char *ptr
= "abcdefghijklmnopqrstuvwxyz";
547 char *res
= strndup (ptr
, 21);
548 if (__builtin_object_size (res
, 1) != 22)
553 res
= strndup (ptr
, 32);
554 if (__builtin_object_size (res
, 1) != 27)
560 if (__builtin_object_size (res
, 1) != 27)
565 char *ptr2
= malloc (64);
568 res
= strndup (ptr2
, 21);
569 if (__builtin_object_size (res
, 1) != 22)
574 res
= strndup (ptr2
, 32);
575 if (__builtin_object_size (res
, 1) != 33)
580 res
= strndup (ptr2
, 128);
581 if (__builtin_object_size (res
, 1) != 64)
587 #ifdef __builtin_object_size
588 if (__builtin_object_size (res
, 1) != 27)
590 if (__builtin_object_size (res
, 1) != (size_t) -1)
597 ptr
= "abcd\0efghijklmnopqrstuvwxyz";
599 if (__builtin_object_size (res
, 1) != 5)
603 res
= strndup (ptr
, 24);
604 if (__builtin_object_size (res
, 1) != 5)
608 res
= strndup (ptr
, 2);
609 if (__builtin_object_size (res
, 1) != 3)
613 res
= strdup (&ptr
[4]);
614 if (__builtin_object_size (res
, 1) != 1)
618 res
= strndup (&ptr
[4], 4);
619 if (__builtin_object_size (res
, 1) != 1)
623 res
= strndup (&ptr
[4], 1);
624 if (__builtin_object_size (res
, 1) != 1)
634 __asm ("" : "=r" (l1
) : "0" (l1
));
638 test4 ((char *) s
, 10);