diff --git a/bin/sh/tests/builtins/Makefile b/bin/sh/tests/builtins/Makefile index 82926658e045..6efec15d2f41 100644 --- a/bin/sh/tests/builtins/Makefile +++ b/bin/sh/tests/builtins/Makefile @@ -149,6 +149,7 @@ FILES+= trap13.0 FILES+= trap14.0 FILES+= trap15.0 FILES+= trap16.0 +FILES+= trap17.0 FILES+= trap2.0 FILES+= trap3.0 FILES+= trap4.0 diff --git a/bin/sh/tests/builtins/trap17.0 b/bin/sh/tests/builtins/trap17.0 new file mode 100644 index 000000000000..89be893edd1a --- /dev/null +++ b/bin/sh/tests/builtins/trap17.0 @@ -0,0 +1,10 @@ +# $FreeBSD$ +# This use-after-free bug probably needs non-default settings to show up. + +v1=nothing v2=nothing +trap 'trap "echo bad" USR1 +v1=trap_received +v2=trap_invoked +:' USR1 +kill -USR1 "$$" +[ "$v1.$v2" = trap_received.trap_invoked ] diff --git a/bin/sh/trap.c b/bin/sh/trap.c index f562e2785a58..8bfebc19ec96 100644 --- a/bin/sh/trap.c +++ b/bin/sh/trap.c @@ -412,6 +412,7 @@ onsig(int signo) void dotrap(void) { + struct stackmark smark; int i; int savestatus, prev_evalskip, prev_skipcount; @@ -445,7 +446,9 @@ dotrap(void) last_trapsig = i; savestatus = exitstatus; - evalstring(trap[i], 0); + setstackmark(&smark); + evalstring(stsavestr(trap[i]), 0); + popstackmark(&smark); /* * If such a command was not