Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 3 Jan 1999 07:15:52 +0800 (CST)
From:      Chia-liang Kao <clkao@CirX.ORG>
To:        freebsd-questions@FreeBSD.ORG
Subject:   setjmp/longjmp corrupts stack?
Message-ID:  <199901022315.HAA00880@genius.cirx.org>

next in thread | raw e-mail | index | archive | help

Hi,

I have a little program attached below causing SIGSEGV.  But the
program works out dramatically if the function being called in main()
(haha()) changes to hehe().

In my track record, the stack corrupted right after longjmp to j2.
But if I change the haha() in main() to hehe(), although the result is
as expected, the stack is somewhat corrupted too. Like the following:

(gdb) bt
#0  haha () at testjmp.c:18
#1  0x804852d in main () at testjmp.c:35
#2  0xefbfd704 in ?? ()
#3  0x6b6c633d in ?? ()
Error accessing memory address 0x52455355: Bad address.

The situation is met also when calling longjmp to j2, too.

My box is 3.0-CURRENT FreeBSD 3.0-CURRENT #2: Sat Jan  2 05:26:13 CST 1999.

The result tested on Linux 2.0.34 is the same; while it works as
expected(well, it's just my expectation, perhaps the POSIX definition
is not as what I thought. But I can't find any other useful info on
man pages either) on Solaris 2.6.

Regards, 
CLK

======================
#include <setjmp.h>
#include <stdio.h>

jmp_buf j1, j2;

void
haha()
{
	int r;
	static int cnt;
	/* ... */
	printf("send\n");
	if(!(r =setjmp(j2))) {
		/* go back */
		longjmp(j1, ++cnt);
	}
	/* resume */
	printf("resume\n");
	return;
}

void
hehe()
{
	haha();
}

int
main()
{
	int r;
	if((r = setjmp(j1))) {
		printf("jmp %d\n", r);
		if(r == 1)
			longjmp(j2, 1);
		else
			exit(0);
	}
	printf("main\n");
	haha();
	printf("after longjmp\n");
	return 0;
}

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199901022315.HAA00880>