2008-11-15 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / printf-chk-1.c
blob8f9a79c5c578a0e78cc730411194c97889da4c77
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
5 volatile int should_optimize;
7 int
8 __attribute__((noinline))
9 __printf_chk (int flag, const char *fmt, ...)
11 va_list ap;
12 int ret;
13 #ifdef __OPTIMIZE__
14 if (should_optimize)
15 abort ();
16 #endif
17 should_optimize = 1;
18 va_start (ap, fmt);
19 ret = vprintf (fmt, ap);
20 va_end (ap);
21 return ret;
24 int
25 main (void)
27 #define test(ret, opt, args...) \
28 should_optimize = opt; \
29 __printf_chk (1, args); \
30 if (!should_optimize) \
31 abort (); \
32 should_optimize = 0; \
33 if (__printf_chk (1, args) != ret) \
34 abort (); \
35 if (!should_optimize) \
36 abort ();
37 test (5, 0, "hello");
38 test (6, 1, "hello\n");
39 test (1, 1, "a");
40 test (0, 1, "");
41 test (5, 0, "%s", "hello");
42 test (6, 1, "%s", "hello\n");
43 test (1, 1, "%s", "a");
44 test (0, 1, "%s", "");
45 test (1, 1, "%c", 'x');
46 test (7, 1, "%s\n", "hello\n");
47 test (2, 0, "%d\n", 0);
48 return 0;