From e3d91ffa909b3618c509bc5e6b7d289c8878f740 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Wed, 24 Nov 2021 01:11:55 +0100 Subject: [PATCH] nv_associative(): finally use proper check for enum (re: b98e32fc) As of the previous commit, I finally know how to properly check for a variable of a type created by 'enum'. We need to check for both the NV_UINT16 attribute and the ENUM_disc discipline. Also: - regression test tweaks - add missing tests for previous commit (f600a5ea) --- src/cmd/ksh93/sh/array.c | 8 ++++---- src/cmd/ksh93/tests/arrays2.sh | 4 +++- src/cmd/ksh93/tests/enum.sh | 23 ++++++++++++++++++----- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/cmd/ksh93/sh/array.c b/src/cmd/ksh93/sh/array.c index 3ea839204..a960f87f8 100644 --- a/src/cmd/ksh93/sh/array.c +++ b/src/cmd/ksh93/sh/array.c @@ -1777,11 +1777,11 @@ void *nv_associative(register Namval_t *np,const char *sp,int mode) if(sh.subshell) np = sh_assignok(np,1); /* - * type == NV_UINT16 (16-bit unsigned integer, see include/nval.h) indicates an - * associative array of a type created by the enum command. nelem should not be - * increased in that case or 'unset' will fail to completely unset such an array. + * For enum types (NV_UINT16 with discipline ENUM_disc), nelem should not + * not increased or 'unset' will fail to completely unset such an array. */ - if(type != NV_UINT16 && (!ap->header.scope || !nv_search(sp,dtvnext(ap->header.table),0))) + if((!ap->header.scope || !nv_search(sp,dtvnext(ap->header.table),0)) + && !(type==NV_UINT16 && nv_hasdisc(np, &ENUM_disc))) ap->header.nelem++; if(nv_isnull(mp)) { diff --git a/src/cmd/ksh93/tests/arrays2.sh b/src/cmd/ksh93/tests/arrays2.sh index 6b273d66e..9094da06f 100755 --- a/src/cmd/ksh93/tests/arrays2.sh +++ b/src/cmd/ksh93/tests/arrays2.sh @@ -238,7 +238,9 @@ function foo { } foo EOF -$SHELL "$multiarray_unset" > /dev/null || err_exit 'Multidimensional arrays with an unset method crash ksh' +{ $SHELL "$multiarray_unset" > /dev/null; } 2>/dev/null +let "(e=$?)==0" || err_exit 'Multidimensional arrays with an unset method crash ksh' \ + "(got status $e$( ((e>128)) && print -n /SIG && kill -l "$e"))" # ====== # Multidimensional indexed array arithmetic assignment operation tests diff --git a/src/cmd/ksh93/tests/enum.sh b/src/cmd/ksh93/tests/enum.sh index 179c774a7..c67e3a498 100755 --- a/src/cmd/ksh93/tests/enum.sh +++ b/src/cmd/ksh93/tests/enum.sh @@ -22,7 +22,7 @@ . "${SHTESTS_COMMON:-${0%/*}/_common}" enum Color_t=(red green blue orange yellow) -enum -i Sex_t=(Male Female) +enum -i Bool_t=(False True) for ((i=0; i < 1000; i++)) do Color_t x @@ -47,10 +47,10 @@ z[green]=xyz z[orange]=bam [[ ${!z[@]} == 'green orange' ]] || err_exit '${!z[@]} == "green orange"' unset x -Sex_t x -[[ $x == Male ]] || err_exit 'Sex_t not defaulting to Male' -x=female -[[ $x == Female ]] || err_exit 'Sex_t not case sensitive' +Bool_t x +[[ $x == False ]] || err_exit 'Bool_t not defaulting to False' +x=true +[[ $x == True ]] || err_exit 'Bool_t not case sensitive' unset x y z done ( @@ -133,5 +133,18 @@ got=$("$SHELL" -c 'source ./cmd.sh' 2>&1) [[ $got == "$exp" ]] || err_exit "sourced script failed" \ "(expected $(printf %q "$exp"); got $(printf %q "$got"))" +# ====== +# https://github.com/ksh93/ksh/issues/335 +unset a +Color_t a + +let "a=5" 2>/dev/null && err_exit "arithmetic can assign out of range (positive)" +let "a=-1" 2>/dev/null && err_exit "arithmetic can assign out of range (negative)" +a=yellow; let "a++" 2>/dev/null && err_exit "arithmetic can assign out of range (increment)" +a=red; let "a--" 2>/dev/null && err_exit "arithmetic can assign out of range (decrement)" +a=orange; let "a+=2" 2>/dev/null && err_exit "arithmetic can assign out of range (add)" +a=green; let "a-=2" 2>/dev/null && err_exit "arithmetic can assign out of range (subtract)" +a=blue; let "a*=3" 2>/dev/null && err_exit "arithmetic can assign out of range (multiply)" + # ====== exit $((Errors<125?Errors:125))