From c1994b87f19995fcc52079322ac6e26cde70d4be Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Mon, 22 Jun 2020 16:27:05 -0700 Subject: [PATCH] Fix nested functions ignoring prefixed variable assignments (#37) This commit fixes the bug described in att/ast#32. The fix and following explanation is from att/ast#467: While copying variables from function's local scope to a new scope, variable attributes were not copied. Such variables were not marked to be exported in the new function. For e.g. function f2 { env | grep -i "^foo"; } function f1 { env | grep -i "^foo"; f2; } foo=bar f1 prints 'foo=bar' only once, but it should print be twice. src/cmd/ksh93/sh/xec.c: - When variables from the local scope of a function are copied into the scope of a nested function, the attributes of the variables need to be copied as well. src/cmd/ksh93/tests/functions.sh: - Add regression tests from ksh2020 to check environment variables passed to functions. --- NEWS | 4 ++++ src/cmd/ksh93/sh/xec.c | 3 +++ src/cmd/ksh93/tests/functions.sh | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/NEWS b/NEWS index 0570f0dc6..6ad414e57 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,10 @@ Any uppercase BUG_* names are modernish shell bug IDs. - Fixed a bug that caused the kill and stop commands to segfault when given a non-existent job. +- Nested functions no longer ignore variable assignments that were prefixed + to their parent function, i.e. 'VAR=foo func' will now set $VAR to 'foo' + in the scope of any nested function 'func' runs. + 2020-06-20: - Fixed a bug that caused setting the following variables as readonly in diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index 6bb4355eb..cfde49b29 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -3249,7 +3249,10 @@ static void local_exports(register Namval_t *np, void *data) if(nv_isarray(np)) nv_putsub(np,NIL(char*),0); if((cp = nv_getval(np)) && (mp = nv_search(nv_name(np), shp->var_tree, NV_ADD|HASH_NOSCOPE)) && nv_isnull(mp)) + { nv_putval(mp, cp, 0); + mp->nvflag = np->nvflag; + } } /* diff --git a/src/cmd/ksh93/tests/functions.sh b/src/cmd/ksh93/tests/functions.sh index ca51e026d..9ec50bd07 100755 --- a/src/cmd/ksh93/tests/functions.sh +++ b/src/cmd/ksh93/tests/functions.sh @@ -1258,5 +1258,13 @@ $SHELL -c 'PATH=/dev/null; fn() { unset -f fn; true; }; fn; fn' 2> /dev/null $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' +# ====== +# Check if environment variables passed while invoking a function are exported +# https://github.com/att/ast/issues/32 +unset foo +function f2 { env | grep -q "^foo" || err_exit "Environment variable is not propogated from caller function"; } +function f1 { f2; env | grep -q "^foo" || err_exit "Environment variable is not passed to a function"; } +foo=bar f1 + # ====== exit $((Errors<125?Errors:125))