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: Finn Magnusson
Subject: Re: [bug-gawk] Gawk 4.2.0 Unexpected handling of function's input variables
Date: Tue, 28 Nov 2017 08:37:28 +0000 (UTC)

Hi
Thank you so much for this very helpful answer .
I tested gawk 4.2-stable from http://git.savannah.gnu.org/cgit/gawk.git/snapshot/gawk-gawk-4.2-stable.tar.gz and it solved the problem !
Do you know when will there be an official gawk package (gawk 4.2.1 ?) available on https://ftp.gnu.org/gnu/gawk/ with these fixes ?
Regards
Finn



From: Andrew J. Schorr <address@hidden>
To: Finn Magnusson <address@hidden>
Cc: "address@hidden" <address@hidden>
Sent: Monday, November 27, 2017 11:27 PM
Subject: Re: [bug-gawk] Gawk 4.2.0 Unexpected handling of function's input variables

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




reply via email to

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