bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Gawk 4.2.0 Unexpected handling of function's input variab


From: Andrew J. Schorr
Subject: Re: [bug-gawk] Gawk 4.2.0 Unexpected handling of function's input variables
Date: Mon, 27 Nov 2017 17:27:22 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

Thank you for reporting this issue.

On Mon, Nov 27, 2017 at 07:22:48PM +0000, Finn Magnusson wrote:
> I noticed a change in the way that gawk 4.2.0 treats the variables declared 
> as input parameter to a function (compared to previous gawk versions).
> See the below code, please put it in a file x.awk and execute with "gawk -f 
> x.awk" 
> 
> { test($1)}
> function test(var)
> {
>     print 1,var
>     system("echo x > dummyfile")
>     while (getline < "dummyfile"){}    close("dummyfile")
>     system("rm dummyfile")
>     print 2,var
> }
> 
> When executing this code it gives for instance when typing "a" on stdin:
> 1 a
> 2 x
> So the variable "var" is getting changed from its initial value "a" to the 
> new value "x".
> That is because when the function "test" read the file "dummyfile" that 
> contained the letter "x" then the variable $1 is given the value "x" and then 
> this gets reflected into the variable var. So I wonder how come a change of 
> "$1" is affecting "var", should "var" not be local to the "test" function? 
> Note that in all previous gawk versions this does not happen, the input 
> variable "var" keeps its value "a" and does not get changed to "x".
> 
> Is this a bug or is it expected behaviour?

It is a bug that is more simply demonstrated by this tweaked version of
your script:

bash-4.2$ echo a | ./gawk  '
{ test($1)}

function test(var)
{
   print 1,var
   "echo x" | getline
   print 2,var
}
'
1 a
2 x

> If it is expected behaviour, is there a way I can modify my code to prevent 
> this from happening?

This bug has already been fixed in the gawk-4.2-stable branch, so you can 
download an
updated version from there. Or you can apply the attached patch.

Regards,
Andy

Attachment: reassign.patch
Description: Text document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]