1#include <stdio.h> 2#include <stdlib.h> 3#include <stdarg.h> 4 5volatile int should_optimize; 6 7int 8__attribute__((noinline)) 9__fprintf_chk (FILE *f, int flag, const char *fmt, ...) 10{ 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 = vfprintf (f, fmt, ap); 20 va_end (ap); 21 return ret; 22} 23 24int 25main (void) 26{ 27#define test(ret, opt, args...) \ 28 should_optimize = opt; \ 29 __fprintf_chk (stdout, 1, args); \ 30 if (!should_optimize) \ 31 abort (); \ 32 should_optimize = 0; \ 33 if (__fprintf_chk (stdout, 1, args) != ret) \ 34 abort (); \ 35 if (!should_optimize) \ 36 abort (); 37 test (5, 1, "hello"); 38 test (6, 1, "hello\n"); 39 test (1, 1, "a"); 40 test (0, 1, ""); 41 test (5, 1, "%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, 0, "%s\n", "hello\n"); 47 test (2, 0, "%d\n", 0); 48 return 0; 49} 50