diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2012-05-23 15:53:21 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2012-05-23 16:12:50 +0200 |
commit | 169a770faf1922ee2298a30a5276cff752d54ae3 (patch) | |
tree | 737529492c960c54a6dcd1038eddb88c48471954 | |
parent | 10ea54dc508f170d251b31c7c2e163298a414653 (diff) | |
download | barebox-169a770faf1922ee2298a30a5276cff752d54ae3.tar.gz barebox-169a770faf1922ee2298a30a5276cff752d54ae3.tar.xz |
hush: do not convert to return code too early
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>
-rw-r--r-- | common/hush.c | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/common/hush.c b/common/hush.c index 8e8dd03eb1..3ac1d10195 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); |