From a8dd1bbd9d921f8d956a569e4baaf62298a25aed Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Sat, 5 Feb 2022 05:09:25 -0800 Subject: [PATCH] typeset -p: fix output of nonexistent [0]= array element (#451) This fix was backported from ksh 93v- 2012-10-04. src/cmd/ksh93/sh/nvtree.c: nv_outnode(): - If the array is supposed to be empty, do not continue. This avoids outputting a nonexistent [0]= element for empty arrays. Resolves: https://github.com/ksh93/ksh/issues/420 Co-authored-by: Martijn Dekker --- NEWS | 5 +++++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh/nvtree.c | 4 +++- src/cmd/ksh93/tests/arrays.sh | 29 +++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index e3064f29e..36bf81561 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0 Any uppercase BUG_* names are modernish shell bug IDs. +2022-02-04: + +- Fixed a bug in 'typeset -p': for an indexed array variable that is set + but empty, it will no longer output a nonexistent '[0]=' element. + 2022-02-02: - Fixed the -a/allexport option to export all variables that are assigned diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 6a69bd738..4fb00d8dd 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -21,7 +21,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2022-02-02" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2022-02-04" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/sh/nvtree.c b/src/cmd/ksh93/sh/nvtree.c index dbec1557a..03960afed 100644 --- a/src/cmd/ksh93/sh/nvtree.c +++ b/src/cmd/ksh93/sh/nvtree.c @@ -556,9 +556,11 @@ void nv_outnode(Namval_t *np, Sfio_t* out, int indent, int special) Indent = indent; if(ap) { + sfputc(out,'('); + if(array_elem(ap)==0) + return; if(!(ap->nelem&ARRAY_SCAN)) nv_putsub(np,NIL(char*),ARRAY_SCAN); - sfputc(out,'('); if(indent>=0) { sfputc(out,'\n'); diff --git a/src/cmd/ksh93/tests/arrays.sh b/src/cmd/ksh93/tests/arrays.sh index b3e535f01..8b701406c 100755 --- a/src/cmd/ksh93/tests/arrays.sh +++ b/src/cmd/ksh93/tests/arrays.sh @@ -754,5 +754,34 @@ got=$("$SHELL" -c 'test -z ${foo[${bar}..${baz}]}' 2>&1) [[ -z $got ]] || err_exit $'Using ${foo[${bar}..${baz}]} when the variable ${foo} isn\'t set fails in error' \ "(got $(printf %q "$got"))" +# ====== +# Spurious '[0]=' element in 'typeset -p' output for indexed array variable that is set but empty +# https://github.com/ksh93/ksh/issues/420 +# https://github.com/att/ast/issues/69#issuecomment-325435618 +exp='len=0: typeset -a Y_ARR=()' +got=$("$SHELL" -c ' + typeset -a Y_ARR + function Y_ARR.unset { + : + } + print -n "len=${#Y_ARR[@]}: " + typeset -p Y_ARR +') +[[ $exp == $got ]] || err_exit 'Giving an array a .unset discipline function adds a spurious [0] element' \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" + +# https://github.com/att/ast/issues/69#issuecomment-325551035 +exp='len=0: typeset -a X_ARR=()' +got=$("$SHELL" -c ' + typeset -a X_ARR=(aa bb cc) + unset X_ARR[2] + unset X_ARR[1] + unset X_ARR[0] + print -n "len=${#X_ARR[@]}: " + typeset -p X_ARR +') +[[ $exp == $got ]] || err_exit 'Unsetting all elements of an array adds a spurious [0] element' \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" + # ====== exit $((Errors<125?Errors:125))