ssp.c

qhwt+dfly, 06/19/2006 04:25 PM

Download (1.14 KB)

 
1
/*
2
 * SSP tickler
3
 *
4
 * expected results(says "OK")
5
 *   gcc -W -Wall -pipe -O -march=i586 ssp.c && ./a.out
6
 *   gcc -W -Wall -pipe -O2 ssp.c && ./a.out
7
 *   gcc -W -Wall -pipe -O2 -fno-stack-protector -march=i586 ssp.c && ./a.out
8
 *        (bug untriggered without SSP)
9
 *   gcc -W -Wall -pipe -O3 -march=i586 ssp.c && ./a.out
10
 *        (baz[] optimized out?)
11
 *
12
 * unexpected results(says "NG")
13
 *   gcc -W -Wall -pipe -Os -fstack-protector -march=i586 ssp.c && ./a.out
14
 *   gcc -W -Wall -pipe -O2 -fstack-protector -march=i586 ssp.c && ./a.out
15
 *
16
 * NOTES
17
 * - tested on the following compiler:
18
 *        gcc 3.4.5 20050809 (prelease) [DragonFly] (propolice, visibility)
19
 * - only -Os and -O2 give you unexpected result.
20
 * - -march or -mtune set to pentium or better is affected.
21
 * - -fno-strict-aliasing has no effect on the result.
22
 */
23
#include <stdio.h>
24

    
25
int foo;
26
int true_expr = 1;
27

    
28
static int
29
bar(void *p)
30
{
31
        char baz[9];
32
        int val = 1;
33

    
34
        /* just to quiet gcc, no effects on the result */
35
        (void)baz; (void)val;
36

    
37
        if (true_expr && !p)
38
                p = &foo;
39
        if (true_expr && !p)
40
                return 1;
41
        else
42
                return 0;
43
}
44

    
45
int
46
main(void)
47
{
48
        printf("%s\n", bar(NULL) ? "NG" : "OK");
49
        return 0;
50
}
51