From 3e3f6b0f12dd846956e388b27c6afa749f9651bf Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Thu, 18 Jun 2020 02:48:51 +0200 Subject: [PATCH] Restore #22 'unset -f' fix minus segfault (re: b7932e87, 97511748) Applying the fix for 'unset -f' exposed a crashing bug in lookup() in sh/nvdisc.c, which is the function for looking up discipline functions. This is what caused tests/variables.sh to crash. Ref.: https://github.com/ksh93/ksh/issues/23#issuecomment-645699614 src/cmd/ksh93/sh/nvdisc.c: lookup(): - To avoid segfault, check that the function pointer nq->nvalue.rp is actually set before checking if nq->nvalue.rp->running==1. src/cmd/ksh93/sh/xec.c, src/cmd/ksh93/tests/functions.sh: - Uncomment the 'unset -f' fix from b7932e87. Resolves #21 (again). --- NEWS | 8 ++++++++ src/cmd/ksh93/sh/nvdisc.c | 2 +- src/cmd/ksh93/sh/xec.c | 5 ----- src/cmd/ksh93/tests/functions.sh | 2 -- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 28dc7a2c5..41464aa3b 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,14 @@ 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 bug in 'unset -f' was fixed that prevented shell functions from + unsetting themselves while they were running. A POSIX function no longer + crashes when doing so, and a KornShell-style function no longer silently + ignores an 'unset -f' on itself. A function of either form now continues + running after unsetting itself, and is removed at the end of the run. + 2020-06-16: - Passing the '-d' flag to the read builtin will no longer cause the '-r' diff --git a/src/cmd/ksh93/sh/nvdisc.c b/src/cmd/ksh93/sh/nvdisc.c index d054cc4e4..927db34de 100644 --- a/src/cmd/ksh93/sh/nvdisc.c +++ b/src/cmd/ksh93/sh/nvdisc.c @@ -417,7 +417,7 @@ static char* lookup(Namval_t *np, int type, Sfdouble_t *dp,Namfun_t *handle) } if(bp== &block) block_done(bp); - if(nq && nq->nvalue.rp->running==1) + if(nq && nq->nvalue.rp && nq->nvalue.rp->running==1) { nq->nvalue.rp->running=0; _nv_unset(nq,0); diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index d2e660820..6bb4355eb 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -3505,16 +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; -/* - * TODO: fix https://github.com/ksh93/ksh/issues/21 - * Following causes a memory fault in: bin/shtests -p variables - * 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 7b081027c..ca51e026d 100755 --- a/src/cmd/ksh93/tests/functions.sh +++ b/src/cmd/ksh93/tests/functions.sh @@ -1253,12 +1253,10 @@ expect_status=0 # 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' -: <<\end_disabled # TODO: re-enable when https://github.com/ksh93/ksh/issues/21 is fixed $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' -end_disabled # ====== exit $((Errors<125?Errors:125))