1 /* Test whether buffer overflow warnings for __*_chk builtins
2 are emitted properly. */
3 /* { dg-do compile } */
4 /* { dg-options "-O2 -std=gnu99" } */
5 /* { dg-options "-mstructure-size-boundary=8 -O2 -std=gnu99" { target arm-*-* } } */
7 extern void abort (void);
9 #include "../gcc.c-torture/execute/builtins/chk.h"
21 char *p
= &buf1
[10], *q
;
23 memcpy (&buf2
[19], "ab", 1);
24 memcpy (&buf2
[19], "ab", 2); /* { dg-warning "will always overflow" "memcpy" } */
25 vx
= mempcpy (&buf2
[19], "ab", 1);
26 vx
= mempcpy (&buf2
[19], "ab", 2); /* { dg-warning "will always overflow" "mempcpy" } */
27 memmove (&buf2
[18], &buf1
[10], 2);
28 memmove (&buf2
[18], &buf1
[10], 3); /* { dg-warning "will always overflow" "memmove" } */
29 memset (&buf2
[16], 'a', 4);
30 memset (&buf2
[15], 'b', 6); /* { dg-warning "will always overflow" "memset" } */
31 strcpy (&buf2
[18], "a");
32 strcpy (&buf2
[18], "ab"); /* { dg-warning "will always overflow" "strcpy" } */
33 vx
= stpcpy (&buf2
[18], "a");
34 vx
= stpcpy (&buf2
[18], "ab"); /* { dg-warning "will always overflow" "stpcpy" } */
35 strncpy (&buf2
[18], "a", 2);
36 strncpy (&buf2
[18], "a", 3); /* { dg-warning "will always overflow" "strncpy" } */
37 strncpy (&buf2
[18], "abc", 2);
38 strncpy (&buf2
[18], "abc", 3); /* { dg-warning "will always overflow" "strncpy" } */
39 memset (buf2
, '\0', sizeof (buf2
));
40 strcat (&buf2
[18], "a");
41 memset (buf2
, '\0', sizeof (buf2
));
42 strcat (&buf2
[18], "ab"); /* { dg-warning "will always overflow" "strcat" } */
43 sprintf (&buf2
[18], "%s", buf1
);
44 sprintf (&buf2
[18], "%s", "a");
45 sprintf (&buf2
[18], "%s", "ab"); /* { dg-warning "will always overflow" "sprintf" } */
46 sprintf (&buf2
[18], "a");
47 sprintf (&buf2
[18], "ab"); /* { dg-warning "will always overflow" "sprintf" } */
48 snprintf (&buf2
[18], 2, "%d", x
);
49 /* N argument to snprintf is the size of the buffer.
50 Although this particular call wouldn't overflow buf2,
51 incorrect buffer size was passed to it and therefore
52 we want a warning and runtime failure. */
53 snprintf (&buf2
[18], 3, "%d", x
); /* { dg-warning "will always overflow" "snprintf" } */
55 vsprintf (&buf2
[18], "a", ap
);
58 vsprintf (&buf2
[18], "ab", ap
); /* { dg-warning "will always overflow" "vsprintf" } */
61 vsnprintf (&buf2
[18], 2, "%s", ap
);
64 /* See snprintf above. */
65 vsnprintf (&buf2
[18], 3, "%s", ap
); /* { dg-warning "will always overflow" "vsnprintf" } */
70 q
= strcpy (p
, ""); /* { dg-warning "will always overflow" "strcpy" } */
72 /* This invokes undefined behaviour, since we are past the end of buf1. */
74 memset (p
, 'd', 1); /* { dg-warning "will always overflow" "memset" } */
77 memset (q
, 'd', 1); /* { dg-warning "will always overflow" "memset" } */
82 char *str
= "ABCDEFG";
83 typedef struct { char b
[16]; } H
;
85 /* Some brown paper bag bugs found in real applications.
86 This test is here merely for amusement. */
92 strncpy (&c
, str
, 3); /* { dg-warning "will always overflow" "strncpy" } */
94 struct { char b
[4]; } x
;
95 sprintf (x
.b
, "%s", "ABCD"); /* { dg-warning "will always overflow" "sprintf" } */
98 memcpy (&i
, &h
, sizeof (h
)); /* { dg-warning "will always overflow" "memcpy" } */
100 unsigned char buf
[21];
101 memset (buf
+ 16, 0, 8); /* { dg-warning "will always overflow" "memset" } */
103 typedef struct { int i
, j
, k
, l
; } S
;
105 memset (s
, 0, sizeof (S
) * 3); /* { dg-warning "will always overflow" "memset" } */
107 struct T
{ char a
[8]; char b
[4]; char c
[10]; } t
;
108 stpcpy (t
.c
,"Testing..."); /* { dg-warning "will always overflow" "stpcpy" } */
112 memset (b1
, 0, sizeof (b1
));
113 memset (b2
, 0, sizeof (b1
)); /* { dg-warning "will always overflow" "memset" } */