* [PATCH] hush: fix conditional statements exit code
@ 2021-08-10 5:47 Andrej Picej
2021-08-23 13:36 ` Sascha Hauer
0 siblings, 1 reply; 2+ messages in thread
From: Andrej Picej @ 2021-08-10 5:47 UTC (permalink / raw)
To: barebox
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>
---
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
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] hush: fix conditional statements exit code
2021-08-10 5:47 [PATCH] hush: fix conditional statements exit code Andrej Picej
@ 2021-08-23 13:36 ` Sascha Hauer
0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2021-08-23 13:36 UTC (permalink / raw)
To: Andrej Picej; +Cc: barebox
On Tue, Aug 10, 2021 at 07:47:12AM +0200, Andrej Picej wrote:
> 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>
> ---
> 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(-)
Tested and applied, thanks
Sascha
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-08-23 13:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-10 5:47 [PATCH] hush: fix conditional statements exit code Andrej Picej
2021-08-23 13:36 ` Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox