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

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