1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 03:32:24 +00:00

Fix incorrect typeset -L/-R/-Z on input with spaces (re: bdb99741)

The typeset output for -L/-R/-Z seems to be wrong when the input
has leading/trailing spaces. This started occurring after the
dynamic buffer size changes introduced in name.c as part of the
fix for <https://github.com/ksh93/ksh/issues/142>.

Test script:
  typeset -L8  s_date1=" 22/02/09 08:25:01"; echo "$s_date1"
  typeset -R10 s_date1="22/02/09 08:25:01 "; echo "$s_date1"
  typeset -Z10 s_date1="22/02/09 08:25:01 "; echo "$s_date1"

Actual output:
22/02/0
  08:25:01
0008:25:01

Expected output:
22/02/09
9 08:25:01
9 08:25:01

src/cmd/ksh93/sh/name.c: nv_newattr():
- Simplify allocation code, replacing the earlier dynamic buffer
  size calculation with just the greater of the strlen and size.

Resolves: https://github.com/ksh93/ksh/issues/476
Co-authored-by: George Lijo <george.lijo@gmail.com>
This commit is contained in:
Martijn Dekker 2022-05-26 00:04:53 +01:00
parent c2fad38bf8
commit 83baa27ef9
5 changed files with 25 additions and 31 deletions

View file

@ -21,6 +21,7 @@ ksh 93u+m general copyright notice
# atheik <atteh.mailbox@gmail.com> #
# Chase <nicetrynsa@protonmail.ch> #
# Finnbarr P. Murphy <fpm@hotmail.com> #
# George Lijo <george.lijo@gmail.com> #
# Govind Kamat <govind_kamat@yahoo.com> #
# Harald van Dijk <harald@gigawatt.nl> #
# Lev Kujawski <int21h@mailbox.org> #

6
NEWS
View file

@ -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

View file

@ -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. */

View file

@ -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);
cp = (char*)sh_malloc((n >= (unsigned)size ? n : (unsigned)size) + 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';
}
if(sp && (mp=nv_opensub(np)))
{
if(trans)

View file

@ -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