From 24b7fcb7718dde909fb7fc62d1fd7d39a936fe0a Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Sat, 24 Apr 2021 13:25:45 -0700 Subject: [PATCH] Don't detect unset functions with whence (re: 13c57e4b) (#287) src/cmd/ksh93/bltins/whence.c: - The previous commit that fixed 'unset -f' in virtual subshells left one bug. The type builtin (or 'whence -v') could still find the unset function in virtual subshells: $ foo() { echo foo; } $ (unset -f foo; type foo) foo is an undefined function To fix this bug, avoid detecting functions in the whence builtin unless they have the NV_FUNCTION flag. src/cmd/ksh93/tests/subshell.sh: - Add a regression test for using 'type' on a function unset inside of a virtual subshell. --- src/cmd/ksh93/bltins/whence.c | 2 +- src/cmd/ksh93/tests/subshell.sh | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cmd/ksh93/bltins/whence.c b/src/cmd/ksh93/bltins/whence.c index 760c4e6c2..5e12bda25 100644 --- a/src/cmd/ksh93/bltins/whence.c +++ b/src/cmd/ksh93/bltins/whence.c @@ -192,7 +192,7 @@ static int whence(Shell_t *shp,char **argv, register int flags) } /* built-ins and functions next */ bltins: - if(!(flags&F_FLAG) && (np = nv_bfsearch(name, shp->fun_tree, &nq, ¬used)) && !is_abuiltin(np)) + if(!(flags&F_FLAG) && (np = nv_bfsearch(name, shp->fun_tree, &nq, ¬used)) && is_afunction(np)) { if(flags&Q_FLAG) continue; diff --git a/src/cmd/ksh93/tests/subshell.sh b/src/cmd/ksh93/tests/subshell.sh index 2ba5cbe90..a7fc8ef1b 100755 --- a/src/cmd/ksh93/tests/subshell.sh +++ b/src/cmd/ksh93/tests/subshell.sh @@ -699,6 +699,16 @@ got=$( f() { echo WRONG; }; ( unset -f f; PATH=/dev/null f 2>&1 ) ) [[ $got == $exp ]] || err_exit 'unset -f fails in sub-subshell on function set in subshell' \ "(expected match of $(printf %q "$exp"), got $(printf %q "$got"))" +# Functions unset in a subshell shouldn't be detected by type (whence -v) +# https://github.com/ksh93/ksh/pull/287 +notafunc() { + echo 'Failure' +} +exp=Success +got=$(unset -f notafunc; type notafunc 2>/dev/null || echo Success) +[[ $got == "$exp" ]] || err_exit "type/whence -v finds function in virtual subshell after it's unset with 'unset -f'" \ + "(expected $(printf %q "$exp"), got $(printf %q "$got"))" + # ====== # Unsetting or redefining aliases within subshells