[PATCH] hush: fix conditional statements exit code

Andrej Picej andrej.picej at norik.com
Mon Aug 9 22:47:12 PDT 2021


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 at norik.com>
---
This problem/bug was previously discussed on barebox mailing list.
Although a simple workaround was found (explicit exit 0 at the end of
scripts), a decision for fixing Hush was encouraged.
---
 common/hush.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/common/hush.c b/common/hush.c
index 047540132..d80df7a18 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;
 }
 
-- 
2.25.1




More information about the barebox mailing list