[PATCH 1/2] Revert "ash: use alloca to get rid of setjmp"

Ron Yorston rmy at frippery.org
Thu Jul 16 09:50:55 UTC 2015


This reverts commit 072fc60f29ee3a4ff38f095a9ef149b2e820c8c0.
Use of alloca is considered unsafe.

Signed-off-by: Ron Yorston <rmy at pobox.com>
---
 shell/ash.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index f4779ee..6627cec 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -11132,6 +11132,19 @@ readtoken1(int c, int syntax, char *eofmark, int striptabs)
 
 	IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;)
 
+#if __GNUC__
+	/* Avoid longjmp clobbering */
+	(void) &out;
+	(void) "ef;
+	(void) &dblquote;
+	(void) &varnest;
+	(void) &arinest;
+	(void) &parenlevel;
+	(void) &dqvarnest;
+	(void) &oldstyle;
+	(void) &prevsyntax;
+	(void) &syntax;
+#endif
 	startlinno = g_parsefile->linno;
 	bqlist = NULL;
 	quotef = 0;
@@ -11596,16 +11609,30 @@ parsesub: {
 parsebackq: {
 	struct nodelist **nlpp;
 	union node *n;
-	char *str;
+	char *volatile str;
+	struct jmploc jmploc;
+	struct jmploc *volatile savehandler;
 	size_t savelen;
 	smallint saveprompt = 0;
 
+#ifdef __GNUC__
+	(void) &saveprompt;
+#endif
+	if (setjmp(jmploc.loc)) {
+		free(str);
+		exception_handler = savehandler;
+		longjmp(exception_handler->loc, 1);
+	}
+	INT_OFF;
 	str = NULL;
 	savelen = out - (char *)stackblock();
 	if (savelen > 0) {
-		str = alloca(savelen);
+		str = ckmalloc(savelen);
 		memcpy(str, stackblock(), savelen);
 	}
+	savehandler = exception_handler;
+	exception_handler = &jmploc;
+	INT_ON;
 	if (oldstyle) {
 		/* We must read until the closing backquote, giving special
 		 * treatment to some slashes, and then push the string and
@@ -11704,7 +11731,12 @@ parsebackq: {
 	if (str) {
 		memcpy(out, str, savelen);
 		STADJUST(savelen, out);
+		INT_OFF;
+		free(str);
+		str = NULL;
+		INT_ON;
 	}
+	exception_handler = savehandler;
 	USTPUTC(CTLBACKQ, out);
 	if (oldstyle)
 		goto parsebackq_oldreturn;
-- 
2.4.3



More information about the busybox mailing list