trap: fix memory leak in exitshell()
PATCH 1/1 trap: fix memory leak in exitshell()
After dash had executed the exit trap handler, the trap was reset but the pointer was never freed. This leak can be demonstrated by running dash through valgrind and executing the following shell script:
foo() {
true
}
trap foo EXITFix by properly freeing the trap pointer in exitshell().
--- src/trap.c | 1 + 1 file changed, 1 insertion(+)
src/trap.c +1 -0
@@ -389,6 +389,7 @@ exitshell(void) trap[0 ] =NULL ; evalskip =0 ;evalstring (p,0 ); +ckfree (p); } out:/*
Signature · 1 line
2.10.2
Quoted 21 lines · Andreas Bofjall
After dash had executed the exit trap handler, the trap was reset but the pointer was never freed. This leak can be demonstrated by running dash through valgrind and executing the following shell script:
foo() { true } trap foo EXITFix by properly freeing the trap pointer in
exitshell().Signed-off-by Andreas Bofjall <[email protected]>--- src/trap.c | 1 + 1 file changed, 1 insertion(+)src/trap.c +0
-0
src/trap.c exitshell(void) · line 395
Quoted 7 lines
src/trap.c +1
-0@@ -389,6 +389,7 @@ exitshell(void) trap[0 ] =NULL ; evalskip =0 ;evalstring (p,0 ); +ckfree (p); } out:/*
This patch will shut up valgrind in the common case, but does not handle the general case. The command string may contain an error or invoke the exit builtin and in either case the command string will be leaked (SIGINT might be expected to have a similar effect, but behaves strangely from an EXIT trap in dash).
You can probably use the exception handling already present in the function to fix this. Note that ckfree() should only be used while INTOFF is in effect, both to avoid longjmp'ing out of free() and to ensure exactly one free in the presence of interruptions and errors.
Signature · 1 line
Jilles Tjoelker
Quoted 20 lines · Jilles Tjoelker
src/trap.c exitshell(void) · line 395
Quoted 12 lines · Andreas Bofjall
src/trap.c +1
-0@@ -389,6 +389,7 @@ exitshell(void) trap[0 ] =NULL ; evalskip =0 ;evalstring (p,0 ); +ckfree (p); } out:/* This patch will shut up valgrind in the common case, but does not handle the general case. The command string may contain an error or invoke the exit builtin and in either case the command string will be leaked (SIGINT might be expected to have a similar effect, but behaves strangely from an EXIT trap in dash).
Quoted 4 lines
You can probably use the exception handling already present in the function to fix this. Note that
ckfree()should only be used while INTOFF is in effect, both to avoid longjmp'ing out offree()and to ensure exactly one free in the presence of interruptions and errors.
Thanks for the feedback! Would something simple like moving the call to ckfree() to after the out: label and wrapping it in INTOFF/INTON (thereby catching both the normal return path and the exception) be ok, or do you mean something more elaborate?
/Andreas