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