[PATCH] ash: fix error code regression

Ron Yorston rmy at pobox.com
Mon Jan 2 12:50:03 UTC 2017


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>
---
 shell/ash.c                             |  7 +++++--
 shell/ash_test/ash-misc/exitcode2.right |  4 ++++
 shell/ash_test/ash-misc/exitcode2.tests | 13 +++++++++++++
 3 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 shell/ash_test/ash-misc/exitcode2.right
 create mode 100755 shell/ash_test/ash-misc/exitcode2.tests

diff --git a/shell/ash.c b/shell/ash.c
index 430e42a..c531a86 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,12 @@ 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..45094c5
--- /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 12: can't open testfile: 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..c1d61c8
--- /dev/null
+++ b/shell/ash_test/ash-misc/exitcode2.tests
@@ -0,0 +1,13 @@
+# syntax error should return status 2
+cat >test.sh <<EOF
+)
+EOF
+chmod +x test.sh
+./test.sh
+echo Done:$?
+rm -f test.sh
+
+# redirection error with special builtin should return status 1
+rm -f testfile
+(eval cat <testfile)
+echo Done:$?
-- 
2.9.3



More information about the busybox mailing list