From: Andrej Picej <andrej.picej@norik.com>
To: barebox@lists.infradead.org
Subject: [PATCH] hush: fix conditional statements exit code
Date: Tue, 10 Aug 2021 07:47:12 +0200 [thread overview]
Message-ID: <20210810054712.1547433-1-andrej.picej@norik.com> (raw)
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
next reply other threads:[~2021-08-10 5:49 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-10 5:47 Andrej Picej [this message]
2021-08-23 13:36 ` Sascha Hauer
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=20210810054712.1547433-1-andrej.picej@norik.com \
--to=andrej.picej@norik.com \
--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