diff --git a/COPYRIGHT b/COPYRIGHT index 7bff14f9c..8b9de0f04 100644 --- a/COPYRIGHT +++ b/COPYRIGHT @@ -21,6 +21,7 @@ ksh 93u+m general copyright notice # atheik # # Chase # # Finnbarr P. Murphy # +# George Lijo # # Govind Kamat # # Harald van Dijk # # Lev Kujawski # diff --git a/NEWS b/NEWS index 66d203682..c0fa3248d 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,12 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0 Any uppercase BUG_* names are modernish shell bug IDs. +2022-05-25: + +- Fixed a bug introduced on 2021-02-20 that caused incorrect output for + typeset -L/-R/-Z when the variable contained leading or trailing spaces. + Thanks to George Lijo for the report and the fix. + 2022-05-21: - Fixed a bug, present since the beginning of ksh93, that broke backslash line diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index c04cdcf6b..d4af15fd8 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -21,7 +21,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2022-05-21" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2022-05-25" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/sh/name.c b/src/cmd/ksh93/sh/name.c index b0c085d56..0f418aa97 100644 --- a/src/cmd/ksh93/sh/name.c +++ b/src/cmd/ksh93/sh/name.c @@ -2991,36 +2991,8 @@ void nv_newattr (register Namval_t *np, unsigned newatts, int size) if(!*sp) sp--; /* if number was 0, leave one zero */ } n = strlen(sp); - if(size==0 || (newatts&(NV_INTEGER|NV_BINARY))) - { - /* allocate to match existing value for numerics and auto length assignment for -L/R/Z */ - cp = (char*)sh_malloc((size_t)n + 1); - strcpy(cp, sp); - } - else if(size>=n) - { - /* growing string */ - cp = (char*)sh_malloc((size_t)size + 1); - strcpy(cp, sp); - } - else - { - /* shrinking string */ - cp = (char*)sh_malloc((size_t)size + 1); - if(newatts&NV_RJUST) - strncpy(cp, n - size + sp, size); - else - { - /* NV_LJUST and the rest */ - if(newatts&NV_ZFILL) - { - while(*sp=='0') sp++; /* skip initial zeros */ - if(!*sp) sp--; /* if number was 0, leave one zero */ - } - strncpy(cp, sp, size); - } - cp[size] = '\0'; - } + cp = (char*)sh_malloc((n >= (unsigned)size ? n : (unsigned)size) + 1); + strcpy(cp, sp); if(sp && (mp=nv_opensub(np))) { if(trans) diff --git a/src/cmd/ksh93/tests/attributes.sh b/src/cmd/ksh93/tests/attributes.sh index ff5829e4a..025d377c5 100755 --- a/src/cmd/ksh93/tests/attributes.sh +++ b/src/cmd/ksh93/tests/attributes.sh @@ -677,6 +677,21 @@ exp=$'00000\n000\n0000000' [[ $got == "$exp" ]] || err_exit 'failed to zero-fill zero' \ "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +# Bug in versions 2021-02-20 to 2022-05-21: incorrect output for +# typeset -L/-R/-Z when the variable had leading or trailing spaces +# https://github.com/ksh93/ksh/issues/476 +exp='22/02/09' +got=$(typeset -L8 s_date1=" 22/02/09 08:25:01"; echo "$s_date1") +[[ $got == "$exp" ]] || err_exit 'incorrect output for typeset -L8' \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +exp='9 08:25:01' +got=$(typeset -R10 s_date1="22/02/09 08:25:01 "; echo "$s_date1") +[[ $got == "$exp" ]] || err_exit 'incorrect output for typeset -R10' \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +got=$(typeset -Z10 s_date1="22/02/09 08:25:01 "; echo "$s_date1") +[[ $got == "$exp" ]] || err_exit 'incorrect output for typeset -Z10' \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" + # ====== # Applying the readonly attribute to an existing variable having a non-numeric attribute with a # size such as -L, -R, or -Z would be set to 0 when it should have maintained the old size unless