From b7932e87b637b885423aafcd63108d04f0c204c6 Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Wed, 17 Jun 2020 10:26:43 -0700 Subject: [PATCH] Fix two problems with 'unset -f' behavior (#22) src/cmd/ksh93/sh/name.c: - Correct the check for when a function is currently running to fix a segmentation fault that occurred when a POSIX function tries to unset itself while it is running. This bug fix was backported from ksh93v-. src/cmd/ksh93/sh/xec.c: - If a function tries to unset itself, unset the function with '_nv_unset(np, NV_RDONLY)' to fix a silent failure. This fix was also backported from ksh93v-. src/cmd/ksh93/tests/functions.sh: - Add four regression tests for when a function unsets itself. Resolves #21 --- NEWS | 9 +++++++++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh/name.c | 2 +- src/cmd/ksh93/sh/xec.c | 5 +++++ src/cmd/ksh93/tests/functions.sh | 9 +++++++++ 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 28dc7a2c5..110ed872e 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,15 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2020-06-17: + +- A POSIX function that unsets itself while it is running no longer causes + a segmentation fault. + +- `unset -f` no longer silently fails when a KornShell style function tries + to unset itself while it is running. The function will now be unset once + it has finished running. + 2020-06-16: - Passing the '-d' flag to the read builtin will no longer cause the '-r' diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 7edbe77ec..69bca002a 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -17,4 +17,4 @@ * David Korn * * * ***********************************************************************/ -#define SH_RELEASE "93u+m 2020-06-16" +#define SH_RELEASE "93u+m 2020-06-17" diff --git a/src/cmd/ksh93/sh/name.c b/src/cmd/ksh93/sh/name.c index a6f078c5f..82890ac6b 100644 --- a/src/cmd/ksh93/sh/name.c +++ b/src/cmd/ksh93/sh/name.c @@ -2509,7 +2509,7 @@ void _nv_unset(register Namval_t *np,int flags) if(is_afunction(np) && np->nvalue.ip) { register struct slnod *slp = (struct slnod*)(np->nvenv); - if(shp->st.real_fun == np->nvalue.rp) + if(np->nvalue.rp->running) { np->nvalue.rp->running |= 1; return; diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index 1a0428cee..6bb4355eb 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -3505,6 +3505,11 @@ static void sh_funct(Shell_t *shp,Namval_t *np,int argn, char *argv[],struct arg nv_putval(SH_PATHNAMENOD,shp->st.filename,NV_NOFREE); shp->pipepid = pipepid; np->nvalue.rp->running -= 2; + if(np->nvalue.rp && np->nvalue.rp->running==1) + { + np->nvalue.rp->running = 0; + _nv_unset(np, NV_RDONLY); + } } /* diff --git a/src/cmd/ksh93/tests/functions.sh b/src/cmd/ksh93/tests/functions.sh index c467070b3..ca51e026d 100755 --- a/src/cmd/ksh93/tests/functions.sh +++ b/src/cmd/ksh93/tests/functions.sh @@ -1249,5 +1249,14 @@ expect_status=0 [[ $actual == "$expect" ]] || err_exit "autoload function skipped dir test wrong output (expected $(printf %q "$expect"), got $(printf %q "$actual"))" +# ====== +# When a function unsets itself, it should not fail to be unset +$SHELL -c 'PATH=/dev/null; fn() { unset -f fn; true; }; fn' || err_exit 'unset of POSIX function in the calling stack fails' +$SHELL -c 'PATH=/dev/null; function ftest { ftest2; }; function ftest2 { unset -f ftest; }; ftest' || err_exit 'unset of ksh function in the calling stack fails' +$SHELL -c 'PATH=/dev/null; fn() { unset -f fn; true; }; fn; fn' 2> /dev/null +[[ $? != 127 ]] && err_exit 'unset of POSIX function fails when it is still running' +$SHELL -c 'PATH=/dev/null; function fn { unset -f fn; true; }; fn; fn' 2> /dev/null +[[ $? != 127 ]] && err_exit 'unset of ksh function fails when it is still running' + # ====== exit $((Errors<125?Errors:125))