All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] [RFC] cmd: exit: Fix return value
@ 2021-04-06 12:00 Marek Vasut
  2021-04-13 14:30 ` Tom Rini
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Vasut @ 2021-04-06 12:00 UTC (permalink / raw)
  To: u-boot

In case exit is called in a script without parameter, the command
returns -2 ; in case exit is called with a numerical parameter,
the command returns -2 and lower. This leads to the following problem:
=> setenv foo 'echo bar ; exit 1' ; run foo ; echo $?
bar
0
=> setenv foo 'echo bar ; exit 0' ; run foo ; echo $?
bar
0
=> setenv foo 'echo bar ; exit -2' ; run foo ; echo $?
bar
0
That is, no matter what the 'exit' command argument is, the return
value is always 0 and so it is not possible to use script return
value in subsequent tests.

Fix this and simplify the exit command such that if exit is called with
no argument, the command returns 0, just like 'true' in cmd/test.c. In
case the command is called with any argument that is positive integer,
the argument is set as return value.
=> setenv foo 'echo bar ; exit 1' ; run foo ; echo $?
bar
1
=> setenv foo 'echo bar ; exit 0' ; run foo ; echo $?
bar
0
=> setenv foo 'echo bar ; exit -2' ; run foo ; echo $?
bar
0

Note that this does change ABI established in 2004 , although it is
unclear whether that ABI was originally OK or not.

Fixes: c26e454dfc6
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
Cc: Tom Rini <trini@konsulko.com>
---
 cmd/exit.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/cmd/exit.c b/cmd/exit.c
index f40d0686e1e..af9ff56180c 100644
--- a/cmd/exit.c
+++ b/cmd/exit.c
@@ -9,13 +9,10 @@
 
 static int do_exit(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	int r;
-
-	r = 0;
 	if (argc > 1)
-		r = simple_strtoul(argv[1], NULL, 10);
+		return simple_strtoul(argv[1], NULL, 10);
 
-	return -r - 2;
+	return 0;
 }
 
 U_BOOT_CMD(
-- 
2.30.2

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] [RFC] cmd: exit: Fix return value
  2021-04-06 12:00 [PATCH] [RFC] cmd: exit: Fix return value Marek Vasut
@ 2021-04-13 14:30 ` Tom Rini
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Rini @ 2021-04-13 14:30 UTC (permalink / raw)
  To: u-boot

On Tue, Apr 06, 2021 at 02:00:24PM +0200, Marek Vasut wrote:

> In case exit is called in a script without parameter, the command
> returns -2 ; in case exit is called with a numerical parameter,
> the command returns -2 and lower. This leads to the following problem:
> => setenv foo 'echo bar ; exit 1' ; run foo ; echo $?
> bar
> 0
> => setenv foo 'echo bar ; exit 0' ; run foo ; echo $?
> bar
> 0
> => setenv foo 'echo bar ; exit -2' ; run foo ; echo $?
> bar
> 0
> That is, no matter what the 'exit' command argument is, the return
> value is always 0 and so it is not possible to use script return
> value in subsequent tests.
> 
> Fix this and simplify the exit command such that if exit is called with
> no argument, the command returns 0, just like 'true' in cmd/test.c. In
> case the command is called with any argument that is positive integer,
> the argument is set as return value.
> => setenv foo 'echo bar ; exit 1' ; run foo ; echo $?
> bar
> 1
> => setenv foo 'echo bar ; exit 0' ; run foo ; echo $?
> bar
> 0
> => setenv foo 'echo bar ; exit -2' ; run foo ; echo $?
> bar
> 0
> 
> Note that this does change ABI established in 2004 , although it is
> unclear whether that ABI was originally OK or not.
> 
> Fixes: c26e454dfc6
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
> Cc: Tom Rini <trini@konsulko.com>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210413/1a52df89/attachment.sig>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-13 14:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-06 12:00 [PATCH] [RFC] cmd: exit: Fix return value Marek Vasut
2021-04-13 14:30 ` Tom Rini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.