summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorAndrej Picej <andrej.picej@norik.com>2021-08-10 07:47:12 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-10-05 09:03:39 +0200
commit032727c039f296ba3f5865cd37e52bf947bc460e (patch)
tree27df9eb5bb31d4e8b77868ce69c3c93a1f5f5ad1 /common
parent0d0060b2380afc029bb0d91186ac340f7ac1023f (diff)
downloadbarebox-032727c039f296ba3f5865cd37e52bf947bc460e.tar.gz
barebox-032727c039f296ba3f5865cd37e52bf947bc460e.tar.xz
hush: fix conditional statements exit code
Fix conditional statements ("if" , "elif", "while" and "until") exit code when used in scripts. Before the change, when conditional statement evaluated false just before the end of the script, script's exit code would have been 1 (instead of 0), which implies error condition. This is not expected nor desired behavior, so correct it by handling such cases and passing exit code 0 (SUCCESS) instead in such situations. Signed-off-by: Andrej Picej <andrej.picej@norik.com> Link: https://lore.barebox.org/20210810054712.1547433-1-andrej.picej@norik.com Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common')
-rw-r--r--common/hush.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/common/hush.c b/common/hush.c
index 0475401321..d80df7a181 100644
--- a/common/hush.c
+++ b/common/hush.c
@@ -854,7 +854,7 @@ static int run_list_real(struct p_context *ctx, struct pipe *pi)
struct pipe *rpipe;
int flag_rep = 0;
int rcode=0, flag_skip=1;
- int flag_restore = 0;
+ int flag_restore = 0, flag_conditional = 0;
int if_code=0, next_if_code=0; /* need double-buffer to handle elif */
reserved_style rmode, skip_more_in_this_rmode = RES_XXXX;
@@ -966,6 +966,20 @@ static int run_list_real(struct p_context *ctx, struct pipe *pi)
return rcode; /* exit */
}
+ /* Conditional statements like "if", "elif", "while" and "until"
+ * return 1 if conditional is not met. This is standard behavior.
+ * However this does not mean that this value (1) should be
+ * returned as exit code, as it suggests generic error code.
+ * Catch this by raising a flag and check it later on.
+ */
+ if (rcode == 1) {
+ if (rmode == RES_IF || rmode == RES_ELIF ||
+ rmode == RES_WHILE || rmode == RES_UNTIL)
+ flag_conditional = 1;
+ else
+ flag_conditional = 0;
+ }
+
last_return_code = rcode;
if (rmode == RES_IF || rmode == RES_ELIF )
@@ -981,6 +995,13 @@ static int run_list_real(struct p_context *ctx, struct pipe *pi)
(rcode != EXIT_SUCCESS && pi->followup == PIPE_AND) )
skip_more_in_this_rmode = rmode;
}
+
+ /* Substitute exit code in case flag_conditional is set. */
+ if (flag_conditional == 1 && last_return_code == 1) {
+ last_return_code = 0;
+ rcode = 0;
+ }
+
return rcode;
}