Skip to content

Commit 02a14ff

Browse files
authored
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.
1 parent 70f6d75 commit 02a14ff

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎NEWS‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh
33

44
Any uppercase BUG_* names are modernish shell bug IDs.
55

6+
2020-07-31:
7+
8+
- Fixed a bug that caused multidimensional associative arrays to be created
9+
with an extra array member.
10+
611
2020-07-29:
712

813
- On a ksh compiled to use fork(2) to run external commands, a bug has been

‎src/cmd/ksh93/include/version.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
* David Korn *
1818
* *
1919
***********************************************************************/
20-
#define SH_RELEASE "93u+m 2020-07-29"
20+
#define SH_RELEASE "93u+m 2020-07-31"

‎src/cmd/ksh93/sh/array.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ Namarr_t *nv_setarray(Namval_t *np, void *(*fun)(Namval_t*,const char*,int))
10031003
ap->nelem = nelem;
10041004
ap->fun = fun;
10051005
nv_onattr(np,NV_ARRAY);
1006-
if(fp || value)
1006+
if(fp || (value && value!=Empty))
10071007
{
10081008
nv_putsub(np, "0", ARRAY_ADD);
10091009
if(value)

‎src/cmd/ksh93/tests/arrays.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,5 +707,15 @@ unset foo
707707
(typeset -A foo; foo=([bar]=baz [lorem]=ipsum))
708708
[[ -n ${ typeset -p foo; } ]] && err_exit 'Associative array leaks out of subshell'
709709
710+
# ======
711+
# Multidimensional associative arrays shouldn't be created with an extra 0 element
712+
unset foo
713+
typeset -A foo
714+
typeset -A foo[bar]
715+
expect="typeset -A foo=([bar]=() )"
716+
actual="$(typeset -p foo)"
717+
# $expect and $actual are quoted intentionally
718+
[[ "$expect" == "$actual" ]] || err_exit "Multidimensional associative arrays are created with an extra array member (expected $expect, got $actual)"
719+
710720
# ======
711721
exit $((Errors<125?Errors:125))

0 commit comments

Comments
 (0)