Skip to content

Commit

Permalink
sh_reinit(): unset tilde expansion disc (re: 0af8199, 936a193)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
McDutchie committed Jan 27, 2024
1 parent 785c6b9 commit 2b1deeb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ This documents significant changes in the dev branch of ksh 93u+m.
For full details, see the git log at: https://github.com/ksh93/ksh
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}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.1.0-alpha" /* 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. */
Expand Down
7 changes: 2 additions & 5 deletions src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,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;
Expand All @@ -1546,6 +1545,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));
Expand Down Expand Up @@ -1584,9 +1585,6 @@ int sh_reinit(char *argv[])
nv_setattr(np,NV_EXPORT); /* turn off everything except export */
if(cp)
np->nvalue.cp = cp; /* replace by string value */
/* unset discipline */
if(np->nvfun && np->nvfun->disc)
np->nvfun->disc = NULL;
}
else
{
Expand Down Expand Up @@ -1704,7 +1702,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;
}

Expand Down
15 changes: 14 additions & 1 deletion src/cmd/ksh93/tests/tilde.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
# #
Expand Down Expand Up @@ -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=$(<test.out)
unset .sh.tilde # removes discipline functions
exp=~okay
[[ $got == "$exp" ]] || err_exit "child script inherits .sh.tilde discipline" \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"

# ======
exit $((Errors<125?Errors:125))

0 comments on commit 2b1deeb

Please sign in to comment.