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

Fix creation of extra associative array element '0' (#101)

Multidimensional associative arrays are created with an extra array
member named '0', which is set to no value. Reproducer:

$ typeset -A foo
$ typeset -A foo[bar]
$ typeset -p foo
typeset -A foo=([bar]=([0]='') )

The bugfix prevents nv_setarray from creating the extra '[0]' member
when an associative array is empty. This bug was discussed on the old
mailing list:
https://www.mail-archive.com/ast-developers@lists.research.att.com/msg01574.html

src/cmd/ksh93/sh/array.c:
- Do not allow the creation of an extra array member when an array
  is empty.

src/cmd/ksh93/tests/arrays.sh:
- Add a regression test for creating multidimensional associative
  arrays, but use the output from 'typeset -p' instead of fgrep.
This commit is contained in:
Johnothan King 2020-07-31 09:32:09 -07:00 committed by GitHub
parent 70f6d758c0
commit 02a14ff9b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 2 deletions

View file

@ -707,5 +707,15 @@ unset foo
(typeset -A foo; foo=([bar]=baz [lorem]=ipsum))
[[ -n ${ typeset -p foo; } ]] && err_exit 'Associative array leaks out of subshell'
# ======
# Multidimensional associative arrays shouldn't be created with an extra 0 element
unset foo
typeset -A foo
typeset -A foo[bar]
expect="typeset -A foo=([bar]=() )"
actual="$(typeset -p foo)"
# $expect and $actual are quoted intentionally
[[ "$expect" == "$actual" ]] || err_exit "Multidimensional associative arrays are created with an extra array member (expected $expect, got $actual)"
# ======
exit $((Errors<125?Errors:125))