Date: Sun, 3 Jan 1999 11:39:37 -0800 (PST) From: Matthew Dillon <dillon@apollo.backplane.com> To: Chia-liang Kao <clkao@CirX.ORG> Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: setjmp/longjmp corrupts stack? Message-ID: <199901031939.LAA51723@apollo.backplane.com>
index | next in thread | raw e-mail
:Hi,
:
:I have a little program attached below causing SIGSEGV. But the
:program works out (partially, see below) dramatically if the function
:being called in main() (haha()) changes to hehe().
:
:In my trace 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:
:...
What you are doing is totally illegal. You cannot push down the
stack, setjmp, return from that context, then longjmp() back down
into it.
The only legal way to use setjmp()/longjmp() is for the context that
you setjmp() to remain *valid* from the point setjmp() is called to the
point longjmp() is called to get back to it.
In the code below you setjmp j1 in main, then call haha which
setjmp's j2 and longjmp's back to j1 (which is ok), but that means
you've popped the haha() context. Then your main function longjmp's back
to j2, inside haha(), which is now totally illegal because that context
was popped when you longjmp'd out of it the first time.
-Matt
: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-hackers" in the body of the message
:
Matthew Dillon Engineering, HiWay Technologies, Inc. & BEST Internet
Communications & God knows what else.
<dillon@backplane.com> (Please include original email in any response)
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199901031939.LAA51723>
