mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH] hush: do not convert to return code too early
Date: Wed, 23 May 2012 16:13:03 +0200	[thread overview]
Message-ID: <1337782383-6236-1-git-send-email-s.hauer@pengutronix.de> (raw)

parse_stream_outer used to convert a exit value to a return code,
but parse_stream_outer maybe inside a recursion. This means that
the exit status is lost in this case. Test case:

if [ 0 = 0 ]; then
	false
	exit $?
fi

echo "shouldn't be here"

Without this patch "shouldn't be here" will be printed.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/hush.c |   16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/common/hush.c b/common/hush.c
index 8e8dd03..3ac1d10 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -757,13 +757,14 @@ static int run_pipe_real(struct p_context *ctx, struct pipe *pi)
 	if (child->sp) {
 		char * str = NULL;
 		struct p_context ctx1;
+		int rcode;
 
 		str = make_string((child->argv + i));
-		parse_string_outer(&ctx1, str, FLAG_EXIT_FROM_LOOP | FLAG_REPARSING);
+		rcode = parse_string_outer(&ctx1, str, FLAG_EXIT_FROM_LOOP | FLAG_REPARSING);
 		release_context(&ctx1);
 		free(str);
 
-		return last_return_code;
+		return rcode;
 	}
 
 	do_glob_in_argv(&globbuf, child->argc - i, &child->argv[i]);
@@ -1603,7 +1604,7 @@ static int parse_stream_outer(struct p_context *ctx, struct in_str *inp, int fla
 			}
 			if (code < -1) {	/* exit */
 				b_free(&temp);
-				return -code - 2;
+				return code;
 			}
 		} else {
 			if (ctx->old_flag != 0) {
@@ -1642,11 +1643,12 @@ static int parse_string_outer(struct p_context *ctx, const char *s, int flag)
 		setup_string_in_str(&input, p);
 		rcode = parse_stream_outer(ctx, &input, flag);
 		free(p);
-		return rcode;
 	} else {
 		setup_string_in_str(&input, s);
-		return parse_stream_outer(ctx, &input, flag);
+		rcode = parse_stream_outer(ctx, &input, flag);
 	}
+
+	return rcode;
 }
 
 static char *insert_var_value(char *inp)
@@ -1793,6 +1795,8 @@ static int source_script(const char *path, int argc, char *argv[])
 	}
 
 	ret = parse_string_outer(&ctx, script, FLAG_PARSE_SEMICOLON);
+	if (ret < -1)
+		ret = -ret - 2;
 
 	release_context(&ctx);
 	free(script);
@@ -1809,6 +1813,8 @@ int run_shell(void)
 	do {
 		setup_file_in_str(&input);
 		rcode = parse_stream_outer(&ctx, &input, FLAG_PARSE_SEMICOLON);
+		if (rcode < -1)
+			rcode = -rcode - 2;
 		release_context(&ctx);
 	} while (!input.__promptme);
 
-- 
1.7.10


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

                 reply	other threads:[~2012-05-23 14:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1337782383-6236-1-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox