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

Fix 'test'/'[' exit status >1 on error in arithmetic expression

Fix BUG_TESTERR1A: POSIX non-compliance of 'test'/'[' exit status
on error. The command now returns status 2 instead of 1 when given
an invalid number or arithmetic expression, e.g.: [ 123 -eq 123x ]

The problem was that the test builtin (b_test()) calls the generic
arithmetic evaluation subsystem (sh/arith.c, sh/streval.c) which
has no awareness of the test builtin. A simple solution would be to
always make the arithmetic subsystem use an exit status > 1 for
arithmetic errors, but globally changing this may cause backwards
compatibility issues. So it's best to change the behaviour of the
'test' builtin only. This requires the arithmetic subsystem to be
aware of whether it was called from the 'test' builtin or not. To
that end, this commit adds a global flag and overrides the
ERROR_exit macro where needed.

src/cmd/ksh93/include/defs.h,
src/cmd/ksh93/sh/defs.c:
- Declare and initialise a global sh_in_test_builtin flag.
- Declare internal function for ERROR_exit override in test.c.

src/cmd/ksh93/bltins/test.c:
- Add override for ERROR_exit macro using a function that checks if
  the exit status is at least 2 if the error occurred while running
  the test builtin.
- b_test(): Set sh_in_test_builtin flag while running test builtin.

src/cmd/ksh93/sh/arith.c,
src/cmd/ksh93/sh/streval.c:
- Override ERROR_exit macro using function from test.c.

src/cmd/ksh93/tests/bracket.sh:
- Add regression test verifying status > 1 on arith error in test.

(cherry picked from commit 5eeae5eb9fd5ed961a5096764ad11ab870a223a9)
This commit is contained in:
Martijn Dekker 2020-05-30 15:21:30 +01:00
parent e902633abe
commit 7003aba487
9 changed files with 73 additions and 13 deletions

View file

@ -31,6 +31,9 @@
#include "variables.h"
#include "builtins.h"
/* POSIX requires error status > 1 if called from test builtin */
#define ERROR_exit(n) _ERROR_exit_b_test(n)
#ifndef LLONG_MAX
#define LLONG_MAX LONG_MAX
#endif

View file

@ -46,3 +46,4 @@ char *sh_lexstates[ST_NONE] = {0};
struct jobs job = {0};
int32_t sh_mailchk = 600;
int sh_in_test_builtin = 0;

View file

@ -36,6 +36,9 @@
#include "FEATURE/externs"
#include "defs.h" /* for sh.decomma */
/* POSIX requires error status > 1 if called from test builtin */
#define ERROR_exit(n) _ERROR_exit_b_test(n)
#ifndef ERROR_dictionary
# define ERROR_dictionary(s) (s)
#endif