From 02a14ff9b7c21329e8a58de631f5215952cb4698 Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Fri, 31 Jul 2020 09:32:09 -0700 Subject: [PATCH] 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. --- NEWS | 5 +++++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh/array.c | 2 +- src/cmd/ksh93/tests/arrays.sh | 10 ++++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 77e8d560f9ff..0b67a4d0ebe2 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2020-07-31: + +- Fixed a bug that caused multidimensional associative arrays to be created + with an extra array member. + 2020-07-29: - On a ksh compiled to use fork(2) to run external commands, a bug has been diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index eb535a6dd7f2..740def0ed1a7 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -17,4 +17,4 @@ * David Korn * * * ***********************************************************************/ -#define SH_RELEASE "93u+m 2020-07-29" +#define SH_RELEASE "93u+m 2020-07-31" diff --git a/src/cmd/ksh93/sh/array.c b/src/cmd/ksh93/sh/array.c index 325732784da5..52a559d6fa6b 100644 --- a/src/cmd/ksh93/sh/array.c +++ b/src/cmd/ksh93/sh/array.c @@ -1003,7 +1003,7 @@ Namarr_t *nv_setarray(Namval_t *np, void *(*fun)(Namval_t*,const char*,int)) ap->nelem = nelem; ap->fun = fun; nv_onattr(np,NV_ARRAY); - if(fp || value) + if(fp || (value && value!=Empty)) { nv_putsub(np, "0", ARRAY_ADD); if(value) diff --git a/src/cmd/ksh93/tests/arrays.sh b/src/cmd/ksh93/tests/arrays.sh index 88c1d9de4208..463ecd93084b 100755 --- a/src/cmd/ksh93/tests/arrays.sh +++ b/src/cmd/ksh93/tests/arrays.sh @@ -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))