Debian Codegraph @

gcc/testsuite/gcc.c-torture/execute/printf-chk-1.c

raw
1 /* { dg-skip-if "requires io" { freestanding } } */
2 /* { dg-xfail-run-if {unexpected PTX 'vprintf' return value} { nvptx-*-* } } */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7
8 volatile int should_optimize;
9
10 int
11 __attribute__((noinline))
12 __printf_chk (int flag, const char *fmt, ...)
13 {
14 va_list ap;
15 int ret;
16 #ifdef __OPTIMIZE__
17 if (should_optimize)
18 abort ();
19 #endif
20 should_optimize = 1;
21 va_start (ap, fmt);
22 ret = vprintf (fmt, ap);
23 va_end (ap);
24 return ret;
25 }
26
27 int
28 main (void)
29 {
30 #define test(ret, opt, args...) \
31 should_optimize = opt; \
32 __printf_chk (1, args); \
33 if (!should_optimize) \
34 abort (); \
35 should_optimize = 0; \
36 if (__printf_chk (1, args) != ret) \
37 abort (); \
38 if (!should_optimize) \
39 abort ();
40 test (5, 0, "hello");
41 test (6, 1, "hello\n");
42 test (1, 1, "a");
43 test (0, 1, "");
44 test (5, 0, "%s", "hello");
45 test (6, 1, "%s", "hello\n");
46 test (1, 1, "%s", "a");
47 test (0, 1, "%s", "");
48 test (1, 1, "%c", 'x');
49 test (7, 1, "%s\n", "hello\n");
50 test (2, 0, "%d\n", 0);
51 return 0;
52 }