Skip to contentSkip to conversation

trap: fix memory leak in exitshell()

PATCH New Andreas Bofjall 1 participant 1 message Dec 1, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 EXIT

Fix by properly freeing the trap pointer in exitshell().

---
v2: move ckfree() to out path

 src/trap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
src/trap.c +4 -1
index edb9938..0824223 100644
@@ -378,7 +378,7 @@ void
 exitshell(void)
 {
 	struct jmploc loc;
-	char *p;
+	char *p = NULL;
 
 	savestatus = exitstatus;
 	TRACE(("pid %d, exitshell(%d)\n", getpid(), savestatus));
@@ -391,6 +391,9 @@ exitshell(void)
 		evalstring(p, 0);
 	}
 out:
+	INTOFF;
+	ckfree(p);
+	INTON;
 	/*
 	 * Disable job control so that whoever had the foreground before we
 	 * started can get it back.
Signature · 1 line
2.10.2