Bug 49038

Summary: /bin/sh does not undefine a function when unset is issued
Product: Base System Reporter: Jukka A. Ukkonen <jau>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description Jukka A. Ukkonen 2003-03-08 22:40:07 UTC
	The bourne shell does not undefine a previously declared shell
	function when unset command is given though the manual pages
	so imply.
	This is a serious portability problem for imported shell scripts.
	Just figure how will a script behave, if there is a function
	called exit which invokes another function which in turn tries
	first "unset exit", does its own work and now tries to invoke
	the real exit...

	Exit()
	{
		unset exit

		Logger -stderr "$*"

		exit 0
	}

	exit()
	{
		...
		Exit "EXIT:" "$*"
	}

Fix: 

This is probably simple oversight in forgetting to clean the unset
	name in the function symbol table though the variable name gets
	gets cleared out of the variable symbol table.
How-To-Repeat: 
	sticky()
	{
		echo "STICKY STILL HERE"
		echo "$*"
	}

	unset sticky

	sticky peekaboo
	STICKY STILL HERE
	peekaboo

	The last two lines are the output from the echo.
Comment 1 robert 2003-03-08 23:49:45 UTC
Hello.

> The bourne shell does not undefine a previously declared shell
> function when unset command is given though the manual pages
> so imply.

I think it is pretty clear that a plain "unset name" does not
undefine functions.

From the sh(1) manual page:

"""
unset [-fv] name ...
The specified variables or functions are unset and unexported.
If the -v option is specified or no options are given, the name
arguments are treated as variable names.  If the -f option is
specified, the name arguments are treated as function names.
"""

> This is a serious portability problem for imported shell scripts.

If these shell scripts rely on "unset name" to remove a function
named "name", they should be regarded as unportable.

From POSIX.1-2001:

"""
If neither -f nor -v is specified, name refers to a variable; if a
variable by that name does not exist, it is unspecified whether a
function by that name, if any, shall be unset.
"""

ciao,
-robert
Comment 2 robert freebsd_committer freebsd_triage 2003-03-09 02:38:55 UTC
State Changed
From-To: open->feedback

Waiting on response from originator.
Comment 3 robert freebsd_committer freebsd_triage 2003-03-10 23:40:44 UTC
State Changed
From-To: feedback->closed

The bourne shell in both 4.7-RELEASE and -CURRENT distinguishes 
between variables and functions in its `unset' command. 
To unset functions, the `-f' flag must be explicitly given. 
The problem lay in the documentation of earlier FreeBSD versions. 
Originator considers PR resolved.