Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 31, 2020

Conversation

JohnoKing
Copy link

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

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

# Alternate reproducer
$ typeset -A foo
$ typeset -A foo[bar]
$ typeset -A foo[baz]
$ foo[bar][cat]=1
$ foo[bar][dog]=2
$ foo[baz][mouse]=3
$ foo[baz][fish]=4
$ set | fgrep foo
_='foo[baz]'
foo=([bar]=([cat]=1 [dog]=2) [baz]=([0]='' [fish]=4 [mouse]=3) )

Output from ksh93v- (which fixed this bug):

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

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

JohnoKing and others added 3 commits July 31, 2020 03:41
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.
@McDutchie McDutchie merged commit 02a14ff into ksh93:master Jul 31, 2020
@JohnoKing JohnoKing deleted the fix-assoc-arrays branch July 31, 2020 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants