bug-gawk
[Top][All Lists]
Advanced

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

RE: print: unexpected gawk's behaviour in case indirect function call


From: Tom Gray
Subject: RE: print: unexpected gawk's behaviour in case indirect function call
Date: Wed, 2 Feb 2022 21:57:34 +0000

This version offers some more insight.

Indirect func calls with wrong number of params
    the params at the end of the list get consumed by the func.
Similar effect happens with the print statements. In the example here, print 
tries to operate on 3 params but the param list grows with the extra func 
params. Print eventually operates on the last three params in the list.

Tom

BEGIN {
  fx="length"

  a = "help"
  b = "the"
  c = "programmer"

  x = @fx( a, b, c )
  y = @fx( a )
  z = @fx( a, b )

  print "x=" x
  print "y=" y
  print "z=" z

  print "one liner's:"
  print "1",   "f="   @fx( a, b, c )   # prints  a,  b  length(c)
  print "2",   "f="   @fx( a )         # prints  f=, fx length(a)
  print "3",   "f="   @fx( a, b )      # prints  fx, a  length(b)
}

Output:

x=10
y=4
z=3
one liner's:
help the10
f= length4
length help3



-----Original Message-----
From: bug-gawk <bug-gawk-bounces+tom_gray=keysight.com@gnu.org> On Behalf Of 
Neil R. Ormos
Sent: Wednesday, February 2, 2022 10:45 AM
To: Bug Gawk List <bug-gawk@gnu.org>
Subject: Re: print: unexpected gawk's behaviour in case indirect function call

CAUTION: This message originates from an external sender.

Denis Shirokov wrote:

> i found strange gawk's behaviour that is completely makes me crazy

> example:

>       BEGIN{
>             print "DIRECT:   "      match( a, b )
>             f = "match"
>             print "INDIRECT: "      @f( a, b )
>             print "END" }

> [...] it's looks like it's lost leading
> "INDIRECT: " string and then outputs content of the globvar `f: 
> "match" and THEN indirectly call built-in match() function and output 
> it's result. [...]

Here are two issues probably related to the same pathology that provoked Denis 
Shirokov's report:

================================================================================

1.  This program:

==============================
BEGIN{
  fx="length"

  print "1"      @fx( a, b, c )
  print "2"      @fx( a )
  print "3"      @fx( a, b )

  }
==============================

produces an Abort, Backtrace, and Memory map, with the error message:

  *** Error in `./uuu/gawk/gawk': double free or corruption (fasttop): 
0x0000560570f2d6b0 ***

If the statements are reordered, the Abort goes away.

Tested on Debian Stretch with:

  GNU Awk 5.1.1, API: 3.1 (GNU MPFR 3.1.5, GNU MP 6.1.2)
  (built from ftp.gnu.org/gnu/gawk/gawk-5.1.1.tar.lz)

  GNU Awk 5.1.60, API: 3.2 (GNU MPFR 3.1.5, GNU MP 6.1.2)
  (built from git.savannah.gnu.org)

(Obviously the address in the error message varies.  Let me know if you need 
the backtrace and
map.)

================================================================================

2. When an indirect call is made to a built-in function, the usual mechanism of 
enforcing the expected number of parameters does not operate.  I have tested 
with length() and a few other built-in functions, but not all of them.

For example, this program does not report errors on indirect calls to length() 
and match() with too many and too few parameters, but does issue a spurious 
message about the type of the third argument to match().

==============================
BEGIN{

  fx="usertest"
  printf "\nFunction: %s\n", fx;
  print usertest(1, 2)
  print @fx(1, 2)

  fx="length"
  printf "\nFunction: %s\n", fx;
  print @fx(1, 2)

  fx="match"
  printf "\nFunction: %s\n", fx;
  print @fx()
  print @fx(1, 2, ya, 3)
  print @fx(1, 2, ya)
  print @fx(1, 2, ya, 3)
  }


function usertest( a  ){
  return 1000
  }
==============================

This is the output from Gawk 4.1.4 on Debian Stretch.
====  OUTPUT  ================

Function: usertest
gawk: test7.awk:5: warning: function `usertest' called with more arguments than 
declared
1000
gawk: test7.awk:6: warning: function `usertest' called with more arguments than 
declared
1000

Function: length
1

Function: match
0
1
1
gawk: test7.awk:17: fatal: attempt to use array `ya' in a scalar context 
==============================

In 5.1.1, and 5.1.60(git), this output is produced:
====  OUTPUT  ================

Function: usertest
gawk: test7.awk:5: warning: function `usertest' called with more arguments than 
declared
1000
gawk: test7.awk:6: warning: function `usertest' called with more arguments than 
declared
1000

Function: length
1

Function: match
0
1
gawk: test7.awk:16: fatal: match: third argument is not an array 
==============================

Note that in the latter results, the the fatal error complaining about the 
argument type to
match() is a different message and occurs on the third call (line 16) instead 
of the fourth call (line 17).

Both errors seem bizarre, as though the function parameters are not being 
counted correctly, e.g., off-by-one or counted from the wrong end of the list.  
I have also noticed an interesting pattern in the results of indirect calls to 
length(); let me know if a description would be useful.




reply via email to

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