Bug 431448

Summary: CRM# 1792308 - ksh variable scope when typeset -x
Product: Red Hat Enterprise Linux 5 Reporter: Alan Matsuoka <alanm>
Component: kshAssignee: Tomas Smetana <tsmetana>
Status: CLOSED WONTFIX QA Contact:
Severity: low Docs Contact:
Priority: low    
Version: 5.1CC: serge.bonin, tao
Target Milestone: rc   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2008-06-16 12:46:40 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 391501    

Description Alan Matsuoka 2008-02-04 16:09:39 UTC
Description of problem:

the customer is using RHEL 5.1 and ksh-20060214-1.4, they bring the following
behaviour to our attention as a bug, this is reproducible 100% with RHEL 5.1 and
ksh-20060214-1.4 (so I don't attach (sys|sos)reports for the moment).


Version-Release number of selected component (if applicable):
ksh-20060214-1.4


How reproducible:
always

Steps to Reproduce:


1.If you run the following script (in attach as "typeset.ksh"):

#!/bin/ksh
function f
{
      let N=N-1 || return
      print ${X}${Y}
      typeset -x X=B
      Y=b
      f
}
typeset -i10 N=3
typeset -x X=A
typeset -x Y=a
f

you get the following output:

Aa
Ab

But the output the customer expects (and that, as far as I can see from the
commands in ksh) should be:

Aa
Bb

I'm not an expert of ksh, but from my understanding the behaviour of this script
is similar to the bash script that I've created and attached ("typeset.sh"):

#!/bin/bash
f ()
{
       N=$(($N-1))
       [ $N -eq 0 ] && return
       echo ${X}${Y}
       export X=B
       Y=b
       f
}
N=3
export X=A
export Y=a
f

If you change (in the ksh script) the line:

      typeset -x X=B

with

      X=B

the issue disappears. So probably the issue is connected to the behaviour of
"typeset -x" in (recursive?) subfunctions.

The customer has a workaround, so this is not an important issue for them at the
moment.


  


Additional info:

Comment 1 Serge Bonin 2008-04-23 21:26:13 UTC
I have a similar problem at a client where the following example:

#!/bin/ksh

sub_func()
{
   typeset ATTRIBUT=6

   echo "The function set a private variable to : $ATTRIBUT"
}

typeset ATTRIBUT=1
echo "The following should have the same value before and after a function 
call:"
echo "Before function call: $ATTRIBUT"
sub_func
echo "After function call: $ATTRIBUT"


The previous code returns:

The following should have the same value before and after a function call:
Before function call: 1
The function set a private variable to : 6
After function call: 6


While it returns the following on AIX, HP-UX, OSF1, and Solaris:

The following should have the same value before and after a function call:
Before function call: 1
The function set a private variable to : 6
After function call: 1






Comment 2 Tomas Smetana 2008-04-24 06:24:55 UTC
I don't think any of these are bugs.

The ksh manual page says that functions defined with 'function name' syntax
share the variables with the calling program with an exception of variables
defined with 'typeset' command that are defined as local in the scope of the
function.  Using '-x' has no effect since the function would have its own
environment.  The X variable defined with typeset inside the function
overshadows the variable X from callers environment but only in the scope of the
function.  The next (recursive) call will start with variable X taken from the
original environment since it is a different scope already.

Things are different with functions defined with 'name()' syntax.  Those
functions run completely in the caller's environment (no exception for typeset).
 This also explains the "issue" in comment #1 -- try to define the function as
'function sub_func'...  This is a documented and expected behaviour.

I think the biggest problem here is that the behaviour was different in ksh88
(RHEL-5 comes with ksh93) that is present on AIX, HP-UX, old Solaris (I think
the new OpenSolaris is already shipped with ksh93 as well), etc. which causes
the confusion.

Comment 3 RHEL Program Management 2008-06-02 20:20:05 UTC
This request was evaluated by Red Hat Product Management for inclusion in a Red
Hat Enterprise Linux maintenance release.  Product Management has requested
further review of this request by Red Hat Engineering, for potential
inclusion in a Red Hat Enterprise Linux Update release for currently deployed
products.  This request is not yet committed for inclusion in an Update
release.

Comment 5 RHEL Program Management 2008-06-16 12:46:40 UTC
Development Management has reviewed and declined this request.  You may appeal
this decision by reopening this request.