File: setjmp-3.c

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (53 lines) | stat: -rw-r--r-- 993 bytes parent folder | download
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
52
53
/* Test of setjmp()/longjmp() functions: jmp_buf bounds.
   $Id: setjmp-3.c,v 1.1.2.2 2008/03/24 11:29:55 dmix Exp $	*/

#include <stdlib.h>
#include <setjmp.h>

struct {
    char beg[16];
    jmp_buf env;
    char end[16];
} buf;

volatile int one = 1;		/* to suppress optimization	*/

void foo (void)
{
    longjmp (buf.env, one);
}

int main ()
{
    volatile unsigned char fill = 0;
    void * volatile p;		/* Avr-gcc >= 4.1.2 outputs incorrect
				    warning without 'volatile'.	*/

    do {
	/* filling	*/
	for (p = &buf; p != (void *)&buf + sizeof (buf); p++)
	    *(char *)p = fill;

	/* buf.env usage	*/
	switch (setjmp (buf.env)) {
	    case 1:
		break;
	    case 0:
		foo ();
	    default:
		exit (__LINE__);
	}
	
	/* checking	*/
	for (p = &buf; p != (void *)&buf + sizeof (buf); p++) {
	    if (*(unsigned char *)p != fill
		&& (p < (void *)&buf.env
		    || p >= (void *)&buf.env + sizeof(buf.env)))
	    {
		exit (__LINE__);
	    }
	}
    } while (++fill);
    
    return 0;
}