From 4e979ec92745949293dd0ac0b926cbb9108e9a8d Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Sat, 27 Jan 2024 02:58:46 +0000 Subject: [PATCH] sh_reinit(): unset tilde expansion disc (re: 232b7bff, 936a1939) Scripts without #! path (e.g., bin/package) incorrectly inherited .sh.tilde.* discipline functions, affecting tilde expansion. This is because sh_reinit() fails to reinitialise/unset these. Just calling _nv_unset for SH_TILDENOD is not enough. For some reason, the SH_INIT state bit stops _nv_unset() from unsetting discipline functions associated with variables. src/cmd/ksh93/sh/init.c: sh_reinit(): - Don't bother with SH_INIT state bit. I don't see the benefit. - Unset SH_TILDENOD, which now also unsets its disciplines. - Since _nv_unset() now unsets disciplines, remove separate code for zeroing discipline pointers in other variables. --- NEWS | 5 +++++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh/init.c | 7 ++----- src/cmd/ksh93/tests/tilde.sh | 15 ++++++++++++++- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 61887a685533..3d04ccabb65b 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,11 @@ This documents significant changes in the 1.0 branch of ksh 93u+m. For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0 Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library. +2024-01-27: + +- Fixed: tilde expansion discipline functions (see 2021-03-16) were not + reinitialised when executing a ksh script without a #! path line. + 2024-01-23: - Fixed a rare crash or rare incorrect behaviour in .sh.tilde.{get,set} diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 9ac5ef91c154..233c7aa5fda3 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -18,7 +18,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.9-beta" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2024-01-23" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2024-01-27" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2024 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/init.c b/src/cmd/ksh93/sh/init.c index 5c59b02a2847..c7944b03dfb1 100644 --- a/src/cmd/ksh93/sh/init.c +++ b/src/cmd/ksh93/sh/init.c @@ -1511,7 +1511,6 @@ int sh_reinit(char *argv[]) Dt_t *dp; int nofree; char *savfpath = NULL; - sh_onstate(SH_INIT); sh.subshell = sh.realsubshell = sh.comsub = sh.curenv = sh.jobenv = sh.inuse_bits = sh.fn_depth = sh.dot_depth = 0; sh.envlist = NULL; sh.last_root = sh.var_tree; @@ -1520,6 +1519,8 @@ int sh_reinit(char *argv[]) sfclose(sh.heredocs); sh.heredocs = 0; } + /* Unset tilde expansion disciplines */ + _nv_unset(SH_TILDENOD,NV_RDONLY); /* save FPATH and treat specially */ if(nv_isattr(FPATHNOD,NV_EXPORT)) savfpath = sh_strdup(nv_getval(FPATHNOD)); @@ -1566,9 +1567,6 @@ int sh_reinit(char *argv[]) /* export all attributes except readonly */ nv_offattr(np,NV_RDONLY); } - /* unset discipline */ - if(np->nvfun && np->nvfun->disc) - np->nvfun->disc = NULL; } else { @@ -1688,7 +1686,6 @@ int sh_reinit(char *argv[]) /* call user init function, if any */ if(sh.userinit) (*sh.userinit)(&sh, 1); - sh_offstate(SH_INIT); return 1; } diff --git a/src/cmd/ksh93/tests/tilde.sh b/src/cmd/ksh93/tests/tilde.sh index c695eb780080..3104feb23ebc 100755 --- a/src/cmd/ksh93/tests/tilde.sh +++ b/src/cmd/ksh93/tests/tilde.sh @@ -2,7 +2,7 @@ # # # This software is part of the ast package # # Copyright (c) 1982-2012 AT&T Intellectual Property # -# Copyright (c) 2020-2022 Contributors to ksh 93u+m # +# Copyright (c) 2020-2024 Contributors to ksh 93u+m # # and is licensed under the # # Eclipse Public License, Version 2.0 # # # @@ -182,5 +182,18 @@ exp=/usr/local/src/ksh93/ksh [[ $got == "$exp" ]] || err_exit "error in special builtin disables .sh.tilde discipline" \ "(expected $(printf %q "$exp"), got $(printf %q "$got"))" +# ====== + +.sh.tilde.set() { print -n BAD; } +.sh.tilde.get() { .sh.value=' & WRONG'; } +echo 'echo ~okay' >test.sh +chmod +x test.sh +./test.sh >test.out +got=$(