Andrew Sumner - 2003-10-29

Logged In: YES
user_id=84532

Not quite. Actually, the divide-by-zero was a secondary effect.
This program shows the problem:

BEGIN {
X = 1;
Y = 0;

if (ARGC == 3)
{
X = ARGV[1];
ARGV[1] = "";
Y = ARGV[2];
ARGV[2] = "";

Ok = X <= Y;
printf("Ok = %d\n", Ok);

for (I = X; I <= Y; I++)
{
printf("I = %s (%d)\n", I, I);
}
}
exit;
}

{
}

END {
}

If this is file z.awk, then the following results occur:

> gawk -f z.awk 1 2
Ok = 1
I = 1 (1)
I = 2 (2)

> gawk -f z.awk 2 1
Ok = 0

If I create the executable z using awka, these are the results:

> z 1 2
Ok = 1
I = 0 (0)

> z 2 1
Ok = 1
I = 0 (0)

> If at all possible, a script that demonstrated this would be
> priceless. I've heard mention from others of this bug,
but haven't
> yet seen an example that replicates it, which I need to be
able to
> find the cause.
>
It's caused by the split function when the array variable is
local to a user-defined function. Apparently, some memory
allocation is performed as part of the split but it isn't
freed before the end of the C function that implements the
user-defined function. Anyway, that's my best guess from a
look at the generated code. If I make the array variable
global
it appears that there is no memory leak, since I think you
reallocate as necessary. So the workaround is to always use
global variables for split arrays, but you may want to fix the
leak for local variables (I use local variables whenever
possible,
following the method described in the Awk book of including
extra
arguments in the function definition).