[git commit branch/1_26_stable] ash: fix error code regression

Denys Vlasenko vda.linux at googlemail.com
Tue Jan 10 15:55:51 UTC 2017


commit: https://git.busybox.net/busybox/commit/?id=568cabf98a11e2e594243a309a9ffd7729a2b213
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/1_26_stable

The commit 'ash,hush: set exit code 127 in "sh /does/not/exist" case'
only partly implemented the dash commit '[ERROR] Allow the originator
of EXERROR to set the exit status'.  This resulted in incorrect error
codes for a syntax error:

   $ )
   $ echo $?
   0

or a redirection error for a special builtin:

   $ rm -f xxx
   $ eval cat <xxx
   $ echo $?
   0

Signed-off-by: Ron Yorston <rmy at pobox.com>
Reported-by: Martijn Dekker <martijn at inlv.org>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/ash.c                               |  8 ++++++--
 shell/ash_test/ash-misc/exitcode2.right   |  4 ++++
 shell/ash_test/ash-misc/exitcode2.tests   | 12 ++++++++++++
 shell/hush_test/hush-misc/exitcode2.right |  4 ++++
 shell/hush_test/hush-misc/exitcode2.tests | 12 ++++++++++++
 5 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index 430e42a..7c53946 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -1284,6 +1284,8 @@ ash_msg_and_raise_error(const char *msg, ...)
 {
 	va_list ap;
 
+	exitstatus = 2;
+
 	va_start(ap, msg);
 	ash_vmsg_and_raise(EXERROR, msg, ap);
 	/* NOTREACHED */
@@ -9588,11 +9590,13 @@ evalcommand(union node *cmd, int flags)
 	}
 
 	if (status) {
+ bail:
+		exitstatus = status;
+
 		/* We have a redirection error. */
 		if (spclbltin > 0)
 			raise_exception(EXERROR);
- bail:
-		exitstatus = status;
+
 		goto out;
 	}
 
diff --git a/shell/ash_test/ash-misc/exitcode2.right b/shell/ash_test/ash-misc/exitcode2.right
new file mode 100644
index 0000000..f7cb983
--- /dev/null
+++ b/shell/ash_test/ash-misc/exitcode2.right
@@ -0,0 +1,4 @@
+./test.sh: line 1: syntax error: unexpected ")"
+Done:2
+./exitcode2.tests: line 11: can't open does_not_exist: no such file
+Done:1
diff --git a/shell/ash_test/ash-misc/exitcode2.tests b/shell/ash_test/ash-misc/exitcode2.tests
new file mode 100755
index 0000000..79a6ebd
--- /dev/null
+++ b/shell/ash_test/ash-misc/exitcode2.tests
@@ -0,0 +1,12 @@
+# syntax error should return status 2
+cat >test.sh <<EOF
+)
+EOF
+chmod +x test.sh
+$THIS_SH ./test.sh
+echo Done:$?
+rm -f test.sh
+
+# redirection error with special builtin should return status 1
+(eval cat <does_not_exist)
+echo Done:$?
diff --git a/shell/hush_test/hush-misc/exitcode2.right b/shell/hush_test/hush-misc/exitcode2.right
new file mode 100644
index 0000000..0a57b9b
--- /dev/null
+++ b/shell/hush_test/hush-misc/exitcode2.right
@@ -0,0 +1,4 @@
+hush: syntax error: unexpected )
+Done:2
+hush: can't open 'does_not_exist': No such file or directory
+Done:1
diff --git a/shell/hush_test/hush-misc/exitcode2.tests b/shell/hush_test/hush-misc/exitcode2.tests
new file mode 100755
index 0000000..79a6ebd
--- /dev/null
+++ b/shell/hush_test/hush-misc/exitcode2.tests
@@ -0,0 +1,12 @@
+# syntax error should return status 2
+cat >test.sh <<EOF
+)
+EOF
+chmod +x test.sh
+$THIS_SH ./test.sh
+echo Done:$?
+rm -f test.sh
+
+# redirection error with special builtin should return status 1
+(eval cat <does_not_exist)
+echo Done:$?


More information about the busybox-cvs mailing list