From 92371efca2f6637d7ab679f2403fcaaee54b133d Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Sun, 15 Jan 2012 20:04:05 +0000 Subject: [PATCH] sh: Fix two bugs with case and exit status: * If no pattern is matched, POSIX says the exit status shall be 0 (even if there are command substitutions). * If a pattern is matched and there are no command substitutions, the first command should see the $? from before the case command, not always 0. --- bin/sh/eval.c | 4 +++- tools/regression/bin/sh/builtins/case14.0 | 5 +++++ tools/regression/bin/sh/builtins/case15.0 | 5 +++++ tools/regression/bin/sh/builtins/case16.0 | 7 +++++++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tools/regression/bin/sh/builtins/case14.0 create mode 100644 tools/regression/bin/sh/builtins/case15.0 create mode 100644 tools/regression/bin/sh/builtins/case16.0 diff --git a/bin/sh/eval.c b/bin/sh/eval.c index e3b93a3e61ea..07b0d64828f7 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -378,7 +378,6 @@ evalcase(union node *n, int flags) setstackmark(&smark); arglist.lastp = &arglist.list; oexitstatus = exitstatus; - exitstatus = 0; expandarg(n->ncase.expr, &arglist, EXP_TILDE); for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) { for (patp = cp->nclist.pattern ; patp ; patp = patp->narg.next) { @@ -392,11 +391,14 @@ evalcase(union node *n, int flags) return (NULL); cp = cp->nclist.next; } + if (cp->nclist.body == NULL) + exitstatus = 0; return (cp->nclist.body); } } } popstackmark(&smark); + exitstatus = 0; return (NULL); } diff --git a/tools/regression/bin/sh/builtins/case14.0 b/tools/regression/bin/sh/builtins/case14.0 new file mode 100644 index 000000000000..0338e8a224e3 --- /dev/null +++ b/tools/regression/bin/sh/builtins/case14.0 @@ -0,0 +1,5 @@ +# $FreeBSD$ + +case `false` in +no) exit 3 ;; +esac diff --git a/tools/regression/bin/sh/builtins/case15.0 b/tools/regression/bin/sh/builtins/case15.0 new file mode 100644 index 000000000000..09b0e1133544 --- /dev/null +++ b/tools/regression/bin/sh/builtins/case15.0 @@ -0,0 +1,5 @@ +# $FreeBSD$ + +case x in +`false`) exit 3 ;; +esac diff --git a/tools/regression/bin/sh/builtins/case16.0 b/tools/regression/bin/sh/builtins/case16.0 new file mode 100644 index 000000000000..24303027ba91 --- /dev/null +++ b/tools/regression/bin/sh/builtins/case16.0 @@ -0,0 +1,7 @@ +# $FreeBSD$ + +f() { return 42; } +f +case x in +x) [ $? = 42 ] ;; +esac