3 /* { dg-options "-O2 -fstack-protector" } */
4 /* { dg-require-effective-target fstack_protector } */
8 #define debug(format, args...) printf (format , ## args)
10 extern int sprintf (char *, const char *, ...);
11 #define debug(format, args...)
14 extern void abort (void);
18 Copyright (C) 2009 Canonical, Ltd.
19 Author: Kees Cook <kees@ubuntu.com>
22 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38616
23 https://bugs.launchpad.net/ubuntu/+source/gcc-4.3/+bug/316019
24 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38902
26 gcc -O2 -fstack-protector truncate.c -o truncate
30 Only the first operation fails, so create a new function for each test.
31 Source must be local (literal or stack)
35 __builtin_strcpy (optimized to __builtin_memcpy?)
36 sprintf (direct) (optmized to __builtin_strcpy?)
37 sprintf (via %s) (optmized to __builtin_strcpy?)
41 sprintf (complex format)
45 char *heap
= "1234567890abcdefghijklmnopqrstuvwxyz";
49 #define CHECK(count, a...) \
50 void test##count (void) \
52 char *local = "1234567890abcdefghijklmnopqrstuvwxyz"; \
53 char buffer[1024]=""; \
55 if (__builtin_strcmp(buffer, heap) == 0) { \
56 debug("Okay(%d):\n\t%s\n", count, # a); \
59 debug("Failed(%d):\n\t%s\n", count, # a); \
65 CHECK( 0, __builtin_memcpy (buffer
, "1234567890abcdefghijklmnopqrstuvwxyz", __builtin_strlen("1234567890abcdefghijklmnopqrstuvwxyz")+1); );
66 CHECK( 1, __builtin_memcpy (buffer
, local
, __builtin_strlen(local
)+1); );
67 CHECK( 2, __builtin_memcpy (buffer
, heap
, __builtin_strlen(heap
)+1); );
69 CHECK( 3, __builtin_memmove (buffer
, "1234567890abcdefghijklmnopqrstuvwxyz", __builtin_strlen("1234567890abcdefghijklmnopqrstuvwxyz")+1); );
70 CHECK( 4, __builtin_memmove (buffer
, local
, __builtin_strlen(local
)+1); );
71 CHECK( 5, __builtin_memmove (buffer
, heap
, __builtin_strlen(heap
)+1); );
73 CHECK( 6, __builtin_strcpy (buffer
, "1234567890abcdefghijklmnopqrstuvwxyz"); );
74 CHECK( 7, __builtin_strcpy (buffer
, local
); );
75 CHECK( 8, __builtin_strcpy (buffer
, heap
); );
77 CHECK( 9, sprintf (buffer
, "1234567890abcdefghijklmnopqrstuvwxyz"); );
78 CHECK(10, sprintf (buffer
, local
); );
79 CHECK(11, sprintf (buffer
, heap
); );
81 CHECK(12, sprintf (buffer
, "%s", "1234567890abcdefghijklmnopqrstuvwxyz"); );
82 CHECK(13, sprintf (buffer
, "%s", local
); );
83 CHECK(14, sprintf (buffer
, "%s", heap
); );
85 CHECK(15, __builtin_strcat (buffer
, "1234567890abcdefghijklmnopqrstuvwxyz"); );
86 CHECK(16, __builtin_strcat (buffer
, local
); );
87 CHECK(17, __builtin_strcat (buffer
, heap
); );
92 sprintf (buffer
, "%s", "1234567890abcdefghijklmnopqrstuvwxyz");;
93 if (__builtin_strcmp(buffer
, heap
) == 0) {
94 debug("Okay(%d):\n\t%s\n", -1, "sprintf (buffer, \"%s\", \"1234567890abcdefghijklmnopqrstuvwxyz\");");
97 debug("Failed(%d):\n\t%s\n", -1, "sprintf (buffer, \"%s\", \"1234567890abcdefghijklmnopqrstuvwxyz\");");
102 int main (int argc
, char *argv
[])
117 // wtf, why are these different?!