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: Neil R. Ormos
Subject: Re: print: unexpected gawk's behaviour in case indirect function call
Date: Wed, 2 Feb 2022 12:45:22 -0600 (CST)

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]