From 19c427435b1b5cb57d7e05581af07c6c0ed0f30d Mon Sep 17 00:00:00 2001 From: hyenias <58673227+hyenias@users.noreply.github.com> Date: Sun, 24 Jan 2021 17:45:08 -0500 Subject: [PATCH] typeset: Correct numeric attribute change for floating points (#163) This commit resolves the following incorrect variable assignments: $ unset a; typeset -uF a=2; typeset -p a typeset -X a=0x1.0000000000p+1 $ unset a; typeset -Fu a=2; typeset -p a typeset -X a=0x1.0000000000p+1 $ unset a; typeset -ulF a=2; typeset -p a typeset -l -X a=0x1.0000000000p+1 $ unset a; typeset -Ful a=2; typeset -p a typeset -l -X a=0x1.0000000000p+1 $ unset a; typeset -Eu a=2; typeset -p a typeset -E -X a=2 $ unset a; typeset -Eul a=2; typeset -p a typeset -l -E -X a=2 src/cmd/ksh93/bltins/typeset.c: - If the unsigned option (-u) was provided in conjunction with a floating point (-F) then due to a flag collision with NV_UNSIGN and NV_HEXFLOAT both having the value of NV_LTOU caused the floating point to become a hexadecimal floating point (-X) in error. Also, if a -E option flag was followed with a -u option then the resulting variable would be both a scientific notation and a hexadecimal floating point at the same time. src/cmd/ksh93/tests/attributes.sh: - Add regression tests. Co-authored-by: Martijn Dekker --- NEWS | 3 +++ src/cmd/ksh93/bltins/typeset.c | 10 ++++++++-- src/cmd/ksh93/tests/attributes.sh | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 6c6b3a323..a640a3f50 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,9 @@ Any uppercase BUG_* names are modernish shell bug IDs. 2021-01-24: +- Fixed a bug in 'typeset': combining the -u option with -F or -E caused the + variable to become a hexadecimal floating point in error. + - Fixed: an unquoted variable expansion evaluated in a DEBUG trap action caused IFS field splitting to be deactivated in code executed after the trap action. This bug was introduced in ksh 93t+ 2009-11-30. diff --git a/src/cmd/ksh93/bltins/typeset.c b/src/cmd/ksh93/bltins/typeset.c index 71de5466a..2c1aebcfd 100644 --- a/src/cmd/ksh93/bltins/typeset.c +++ b/src/cmd/ksh93/bltins/typeset.c @@ -274,6 +274,9 @@ int b_typeset(int argc,register char *argv[],Shbltin_t *context) flag &= ~NV_EXPNOTE; flag |= NV_HEXFLOAT; } + else + /* n=='F' Remove possible collision with NV_UNSIGN/NV_HEXFLOAT */ + flag &= ~NV_HEXFLOAT; break; case 'b': flag |= NV_BINARY; @@ -352,8 +355,11 @@ int b_typeset(int argc,register char *argv[],Shbltin_t *context) flag |= NV_TAGGED; break; case 'u': - tdata.wctname = e_toupper; - flag |= NV_LTOU; + if(!isfloat) + { + tdata.wctname = e_toupper; + flag |= NV_LTOU; + } break; case 'x': flag &= ~NV_VARNAME; diff --git a/src/cmd/ksh93/tests/attributes.sh b/src/cmd/ksh93/tests/attributes.sh index 0961ffeb4..fc6ddc5ce 100755 --- a/src/cmd/ksh93/tests/attributes.sh +++ b/src/cmd/ksh93/tests/attributes.sh @@ -587,5 +587,31 @@ got=$(< $tmpfile) [[ $got == "$exp" ]] || err_exit 'variable exported in function in a subshell is also visible in a different subshell' \ "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +# ====== +# Combining -u with -F or -E caused an incorrect variable type +# https://github.com/ksh93/ksh/pull/163 +typeset -A expect=( + [uF]='typeset -F a=2.0000000000' + [Fu]='typeset -F a=2.0000000000' + [ulF]='typeset -l -F a=2.0000000000' + [Ful]='typeset -l -F a=2.0000000000' + [Eu]='typeset -E a=2' + [Eul]='typeset -l -E a=2' +) +for flag in "${!expect[@]}" +do unset a + actual=$( + redirect 2>&1 + (typeset "-$flag" a=2; typeset -p a) + leak=${ typeset -p a; } + [[ -n $leak ]] && print "SUBSHELL LEAK: $leak" + ) + if [[ $actual != "${expect[$flag]}" ]] + then err_exit "typeset -$flag a=2:" \ + "expected $(printf %q "${expect[$flag]}"), got $(printf %q "$actual")" + fi +done +unset expect + # ====== exit $((Errors<125?Errors:125))