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

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 <martijn@inlv.org>
This commit is contained in:
Johnothan King 2022-02-05 05:09:25 -08:00 committed by Martijn Dekker
parent fb696ecfae
commit a8dd1bbd9d
4 changed files with 38 additions and 2 deletions

View file

@ -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. */

View file

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

View file

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