1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Fix BUG_LOOPRET2 and related return/exit misbehaviour

The 'exit' and 'return' commands without an argument failed to pass
down the exit status of the last-run command when incorporated in a
block with redirection, &&/|| list, 'case' statement, or 'while',
'until' or 'for' loop.

src/cmd/ksh93/bltins/cflow.c:
- Use $?, which is sh.savexit a.k.a. shp->savexit, as the default
  exit status value if there is no argument, instead of
  shp->oldexit. This fixes the default exit status behaviour to
  match POSIX and other shells.

src/cmd/ksh93/include/defs.h,
src/cmd/ksh93/include/shell.h:
- Remove now-unused sh.oldexit (a.k.a. shp->oldexit) private struct
  member. It appeared to fulfill the same function as sh.savexit,
  but in a slightly broken way.
- Move the savexit/$? declaration from the _SH_PRIVATE part of the
  struct definition to the public API part. Since $? uses this,
  it's clearly a publicly exposed value already, and this is
  generally the one to use. (If anything, it's exitval that should
  have been private.) This declares savexit right next to exitval,
  rewriting the comments to clarify the difference between them.

src/cmd/ksh93/sh/fault.c,
src/cmd/ksh93/sh/subshell.c,
src/cmd/ksh93/sh/xec.c:
- Remove assignments to shp->oldexit.

src/cmd/ksh93/tests/basic.sh:
- Add thorough regression tests for the default exit status
  behaviour of 'return' and 'exit' in various lexical contexts.
- Verify that 'for' and 'case' without any command, as well as a
  lone redirection, still correctly reset the exit status to 0.

Fixes: #117
This commit is contained in:
Martijn Dekker 2020-09-09 20:02:20 +02:00
parent 400c107773
commit 092b90da81
10 changed files with 98 additions and 15 deletions

View file

@ -155,7 +155,6 @@ struct shared
Sfio_t *heredocs; /* current here-doc temp file */ \
Sfio_t *funlog; /* for logging function definitions */ \
int **fdptrs; /* pointer to file numbers */ \
int savexit; \
char *lastarg; \
char *lastpath; /* last alsolute path found */ \
int path_err; /* last error on path search */ \
@ -181,7 +180,6 @@ struct shared
char *prefix; /* prefix for compound assignment */ \
sigjmp_buf *jmplist; /* longjmp return stack */ \
char *fifo; /* fifo name for process sub */ \
int oldexit; \
pid_t bckpid; /* background process id */ \
pid_t cpid; \
pid_t spid; /* subshell process id */ \

View file

@ -140,7 +140,8 @@ struct Shell_s
Dt_t *bltin_tree; /* for builtin commands */
Shscope_t *topscope; /* pointer to top-level scope */
int inlineno; /* line number of current input file */
int exitval; /* most recent exit value */
int exitval; /* exit status of the command currently being run */
int savexit; /* $? == exit status of the last command executed */
unsigned char trapnote; /* set when trap/signal is pending */
char shcomp; /* set when running shcomp */
unsigned int subshell; /* set for virtual subshell */

View file

@ -17,4 +17,4 @@
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#define SH_RELEASE "93u+m 2020-09-05"
#define SH_RELEASE "93u+m 2020-09-09"