From 50b665b1ed85fbb1e03ec8af879754cc48cd1d9b Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Thu, 18 Feb 2021 01:07:43 +0000 Subject: [PATCH] Revert 0e4c4d61 ("Fix minor typeset attribute regressions") This commit introduced the following bug, which is worse than the one that commit fixed: it became impossible to alter the size of an existing justified string attribute. Thanks to @hyenias for catching this bug: https://github.com/ksh93/ksh/issues/142#issuecomment-780931533 $ unset s; typeset -L 100 s=h; typeset +p s; typeset -L 5 s; typeset +p s typeset -L 100 s typeset -L 100 s Expected output: typeset -L 100 s typeset -L 5 s src/cmd/ksh93/sh/name.c: - Revert. src/cmd/ksh93/tests/attributes.sh: - Revert: re-disable tests for minor attribute output regressions. - Add a test for this bug and potential similar bugs. --- src/cmd/ksh93/sh/name.c | 5 +++-- src/cmd/ksh93/tests/attributes.sh | 21 ++++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/cmd/ksh93/sh/name.c b/src/cmd/ksh93/sh/name.c index 2ea2d009e..db142e9a0 100644 --- a/src/cmd/ksh93/sh/name.c +++ b/src/cmd/ksh93/sh/name.c @@ -2985,10 +2985,11 @@ void nv_newattr (register Namval_t *np, unsigned newatts, int size) nv_onattr(np,NV_EXPORT); sh_envput(shp->env,np); } + if((n^newatts)==NV_EXPORT && !trans) + /* Only EXPORT attribute has changed and thus all work has been done. */ + return; } oldsize = nv_size(np); - if(((n^newatts) & ~(NV_EXPORT|NV_RDONLY))==0) - size = oldsize; if((size==oldsize|| (n&NV_INTEGER)) && !trans && ((n^newatts)&~NV_NOCHANGE)==0) { if(size>0) diff --git a/src/cmd/ksh93/tests/attributes.sh b/src/cmd/ksh93/tests/attributes.sh index 30903eed2..a56f4de88 100755 --- a/src/cmd/ksh93/tests/attributes.sh +++ b/src/cmd/ksh93/tests/attributes.sh @@ -526,16 +526,16 @@ typeset -A expect=( [F12]='typeset -x -r -F 12 foo' [H]='typeset -x -r -H foo' [L]='typeset -x -r -L 0 foo' - [L17]='typeset -x -r -L 17 foo' +# [L17]='typeset -x -r -L 17 foo' # TODO: outputs '-L 0' [Mtolower]='typeset -x -r -l foo' [Mtoupper]='typeset -x -r -u foo' [R]='typeset -x -r -R 0 foo' - [R17]='typeset -x -r -R 17 foo' +# [R17]='typeset -x -r -R 17 foo' # TODO: outputs '-L 0' [X17]='typeset -x -r -X 17 foo' [S]='typeset -x -r foo' [T]='typeset -x -r foo' [Z]='typeset -x -r -Z 0 -R 0 foo' - [Z13]='typeset -x -r -Z 13 -R 13 foo' +# [Z13]='typeset -x -r -Z 13 -R 13 foo' # TODO: outputs 'typeset -x -r -Z 0 -R 0 foo' ) for flag in "${!expect[@]}" do unset foo @@ -614,5 +614,20 @@ do unset a done unset expect +# ====== +# https://github.com/ksh93/ksh/issues/142#issuecomment-780931533 +got=$(unset s; typeset -L 100 s=h; typeset +p s; typeset -L 5 s; typeset +p s) +exp=$'typeset -L 100 s\ntypeset -L 5 s' +[[ $got == "$exp" ]] || err_exit 'cannot alter size of existing justified string attribute' \ + "expected $(printf %q "$exp"), got $(printf %q "$got")" +got=$(unset s; typeset -L 100 s=h; typeset +p s; typeset -R 5 s; typeset +p s) +exp=$'typeset -L 100 s\ntypeset -R 5 s' +[[ $got == "$exp" ]] || err_exit 'cannot set -R after setting -L' \ + "expected $(printf %q "$exp"), got $(printf %q "$got")" +got=$(unset s; typeset -L 100 s=h; typeset +p s; typeset -Z 5 s; typeset +p s) +exp=$'typeset -L 100 s\ntypeset -Z 5 -R 5 s' +[[ $got == "$exp" ]] || err_exit 'cannot set -Z after setting -L' \ + "expected $(printf %q "$exp"), got $(printf %q "$got")" + # ====== exit $((Errors<125?Errors:125))