awk leaking memory during arithmetic?

From: Brian Reichert <reichert_at_numachi.com>
Date: Mon, 4 Oct 2004 13:14:22 -0400
I may be misremembering some awk lore, but this still seems like
undesired behavior.  Essentially, I'm trying to sum up some numbers,
but awk spin, chewing up memory, until it drops a huge core file.

  # uname -a
  FreeBSD backup.internal 5.2.1-RELEASE FreeBSD 5.2.1-RELEASE #1: Mon Sep 27
  19:27:46 EDT 200	 root_at_backup2.internal:/usr/src/sys/i386/compile/FILESERVER  i386
  
  # cat test_list | awk '{print $1}'
  53999616
  53999616
  53311488
  102475776
  257134592
  858624
  512909312
  1147392
  39385174
  35815424

  # cat test_list | awk '{ t += $1 } END {print $t}'
  awk in malloc(): error: allocation failed
  Abort (core dumped)

  # ls -l awk.core 
  -rw-------  1 root  wheel  537698304 Oct  4 12:58 awk.core

Changing the awk program thusly:

  cat test_list | awk '{ t += "$1" } END {print $t }'

Stops the malloc errors, but now prints nothing.

This same test on 4.10-RELEASE also doesn't do what I expect:

  % cat test_list | awk '{ t += $1 } END {print $t}'

  %

Just prints a blank line, with a zero exit status.  Adding the quotes
as above, yeilds some bad math, which I presume is some overflow
error:

  % cat test_list | awk '{ t += "$1" } END {print $t}'
  35815424

I note that 5.2.1 and 4.10 are using different versions of awk.

Are these awk bugs, or am I misusing awk?  The memory consumption
under 5.2.1 is what concerns me the most.  I'll open a PR, if that's
the concensus.  This sypmtom doesn't seem to correspond to any of
the (closed) PRs about awk...

-- 
Brian Reichert				<reichert_at_numachi.com>
37 Crystal Ave. #303			Daytime number: (603) 434-6842
Derry NH 03038-1713 USA			BSD admin/developer at large	
Received on Mon Oct 04 2004 - 15:14:23 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:15 UTC