-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 'unset -f' to work in subshells without forking (re: 047cb33)
This commit implements unsetting functions in virtual subshells, removing the need for the forking workaround. This is done by either invalidating the function found in the current subshell function tree by unsetting its NV_FUNCTION attribute bits (which will cause sh_exec() to skip it) or, if the function exists in a parent shell, by creating an empty dummy subshell node in the current function tree without that attribute. As a beneficial side effect, it seems that bug 228 (unset -f fails in forked subshells if a function is defined before forking) is now also fixed. src/cmd/ksh93/include/defs.h, src/cmd/ksh93/sh/init.c: - Add sh.fun_base for a saved pointer to the main shell's function tree for checking when in a subshell, analogous to sh.var_base. src/cmd/ksh93/bltins/typeset.c: unall(): - Remove the fork workaround. - When unsetting a function found in the current function tree (troot) and that tree is not sh.var_base (which checks if we're in a virtual subshell in a way that handles shared-state command substitutions correctly), then do not delete the function but invalidate it by unsetting its NV_FUNCTION attribute bits. - When unsetting a function not found in the current function tree, search for it in sh.fun_base and if found, add an empty dummy node to mask the parent shell environment's function. The dummy node will not have NV_FUNCTION set, so sh_exec() will skip it. src/cmd/ksh93/sh/subshell.c: - sh_subfuntree(): For 'unset -f' to work correctly with shared-state command substitutions (subshares), this function needs a fix similar to the one applied to sh_assignok() for variables in commit 911d6b0. Walk up on the subshells tree until we find a non-subshare. - sh_subtracktree(): Apply the same fix for the hash table. - Remove table_unset() and incorporate an updated version of its code in sh_subshell(). As of ec88886, this function was only used to clean up the subshell function table as the alias table no longer exists. - sh_subshell(): * Simplify the loop to free the subshell hash table. * Add table_unset() code, slightly refactored for readability. Treat dummy nodes now created by unall() separately to avoid a memory leak; they must be nv_delete()d without passing the NV_FUNCTION bits. For non-dummy nodes, turn on the NV_FUNCTION attribute in case they were invalidated by unall(); this is needed for _nv_unset() to free the function definition. src/cmd/ksh93/tests/subshell.sh: - Update the test for multiple levels of subshell functions to test a subshare as well. While we're add it, add a very similar test for multiple levels of subshell variables that was missing. - Add @JohnoKing's reproducer from #228. src/cmd/ksh93/tests/leaks.sh: - Add leak tests for unsetting functions in a virtual subshell. Test both the simple unset case (unall() creates a dummy node) and the define/unset case (unall() invalidates existing node). Resolves: #228
- Loading branch information
Showing
6 changed files
with
141 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters