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.
This commit is contained in:
Jilles Tjoelker 2012-01-15 20:04:05 +00:00
parent 4aecc339cf
commit 92371efca2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=230154
4 changed files with 20 additions and 1 deletions

View File

@ -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);
}

View File

@ -0,0 +1,5 @@
# $FreeBSD$
case `false` in
no) exit 3 ;;
esac

View File

@ -0,0 +1,5 @@
# $FreeBSD$
case x in
`false`) exit 3 ;;
esac

View File

@ -0,0 +1,7 @@
# $FreeBSD$
f() { return 42; }
f
case x in
x) [ $? = 42 ] ;;
esac