This service will be undergoing maintenance at 00:00 UTC, 2016-08-01. It is expected to last about 1 hours
Bug 431448 - CRM# 1792308 - ksh variable scope when typeset -x
CRM# 1792308 - ksh variable scope when typeset -x
Status: CLOSED WONTFIX
Product: Red Hat Enterprise Linux 5
Classification: Red Hat
Component: ksh (Show other bugs)
5.1
All Linux
low Severity low
: rc
: ---
Assigned To: Tomas Smetana
:
Depends On:
Blocks: 391501
  Show dependency treegraph
 
Reported: 2008-02-04 11:09 EST by Alan Matsuoka
Modified: 2013-04-12 15:31 EDT (History)
2 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2008-06-16 08:46:40 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:


Attachments (Terms of Use)

  None (edit)
Description Alan Matsuoka 2008-02-04 11:09:39 EST
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 17:26:13 EDT
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 02:24:55 EDT
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 Product and Program Management 2008-06-02 16:20:05 EDT
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 Product and Program Management 2008-06-16 08:46:40 EDT
Development Management has reviewed and declined this request.  You may appeal
this decision by reopening this request. 

Note You need to log in before you can comment on or make changes to this bug.