Skip to content

Commit 2b1deeb

Browse files
committed
sh_reinit(): unset tilde expansion disc (re: 0af8199, 936a193)
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.
1 parent 785c6b9 commit 2b1deeb

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

NEWS

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ This documents significant changes in the dev branch of ksh 93u+m.
22
For full details, see the git log at: https://github.com/ksh93/ksh
33
Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.
44

5+
2024-01-27:
6+
7+
- Fixed: tilde expansion discipline functions (see 2021-03-16) were not
8+
reinitialised when executing a ksh script without a #! path line.
9+
510
2024-01-23:
611

712
- Fixed a rare crash or rare incorrect behaviour in .sh.tilde.{get,set}

src/cmd/ksh93/include/version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
2020
#define SH_RELEASE_SVER "1.1.0-alpha" /* semantic version number: https://semver.org */
21-
#define SH_RELEASE_DATE "2024-01-23" /* must be in this format for $((.sh.version)) */
21+
#define SH_RELEASE_DATE "2024-01-27" /* must be in this format for $((.sh.version)) */
2222
#define SH_RELEASE_CPYR "(c) 2020-2024 Contributors to ksh " SH_RELEASE_FORK
2323

2424
/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */

src/cmd/ksh93/sh/init.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,6 @@ int sh_reinit(char *argv[])
15371537
Dt_t *dp;
15381538
int nofree;
15391539
char *savfpath = NULL;
1540-
sh_onstate(SH_INIT);
15411540
sh.subshell = sh.realsubshell = sh.comsub = sh.curenv = sh.jobenv = sh.inuse_bits = sh.fn_depth = sh.dot_depth = 0;
15421541
sh.envlist = NULL;
15431542
sh.last_root = sh.var_tree;
@@ -1546,6 +1545,8 @@ int sh_reinit(char *argv[])
15461545
sfclose(sh.heredocs);
15471546
sh.heredocs = 0;
15481547
}
1548+
/* Unset tilde expansion disciplines */
1549+
_nv_unset(SH_TILDENOD,NV_RDONLY);
15491550
/* save FPATH and treat specially */
15501551
if(nv_isattr(FPATHNOD,NV_EXPORT))
15511552
savfpath = sh_strdup(nv_getval(FPATHNOD));
@@ -1584,9 +1585,6 @@ int sh_reinit(char *argv[])
15841585
nv_setattr(np,NV_EXPORT); /* turn off everything except export */
15851586
if(cp)
15861587
np->nvalue.cp = cp; /* replace by string value */
1587-
/* unset discipline */
1588-
if(np->nvfun && np->nvfun->disc)
1589-
np->nvfun->disc = NULL;
15901588
}
15911589
else
15921590
{
@@ -1704,7 +1702,6 @@ int sh_reinit(char *argv[])
17041702
/* call user init function, if any */
17051703
if(sh.userinit)
17061704
(*sh.userinit)(&sh, 1);
1707-
sh_offstate(SH_INIT);
17081705
return 1;
17091706
}
17101707

src/cmd/ksh93/tests/tilde.sh

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# #
33
# This software is part of the ast package #
44
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5-
# Copyright (c) 2020-2022 Contributors to ksh 93u+m #
5+
# Copyright (c) 2020-2024 Contributors to ksh 93u+m #
66
# and is licensed under the #
77
# Eclipse Public License, Version 2.0 #
88
# #
@@ -182,5 +182,18 @@ exp=/usr/local/src/ksh93/ksh
182182
[[ $got == "$exp" ]] || err_exit "error in special builtin disables .sh.tilde discipline" \
183183
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
184184

185+
# ======
186+
187+
.sh.tilde.set() { print -n BAD; }
188+
.sh.tilde.get() { .sh.value=' & WRONG'; }
189+
echo 'echo ~okay' >test.sh
190+
chmod +x test.sh
191+
./test.sh >test.out
192+
got=$(<test.out)
193+
unset .sh.tilde # removes discipline functions
194+
exp=~okay
195+
[[ $got == "$exp" ]] || err_exit "child script inherits .sh.tilde discipline" \
196+
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
197+
185198
# ======
186199
exit $((Errors<125?Errors:125))

0 commit comments

Comments
 (0)