Subject: bug#13530: head: memory exhausted when printing all from stdin but last P/E bytes

From: Jim Meyering <[email-masked]>
Date: Tue, 28 May 2013 02:07:20 +0200

Pádraig Brady wrote:
...
>> -(ulimit -v 20000; head --bytes=-E < /dev/null) || fail=1
>> +(ulimit -v 20000; head --bytes=-$OFF_T_MAX < /dev/null) || fail=1
>>
>>  Exit $fail
>> --
>> 1.8.3
>>
>
> So the test would have to be adjusted to take the min(SIZE_MAX, OFF_T_MAX)?
>
> HEAD_TAIL_LIMIT=$(printf '%s\n' $SIZE_MAX $OFF_T_MAX | sort -g | head -n1)

I didn't see a reason to include OFF_T_MAX, since this has to do
solely with in-memory buffering, and no seeking.
Also, I had to subtract BUFSIZ, so made another adjustment.

This works for me:

>From 6b20bb5c9c464277f843a1d3f418824275f25f6b Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email-masked]>
Date: Mon, 27 May 2013 17:01:14 -0700
Subject: [PATCH] tests: head-c: avoid spurious failure with a 32-bit SIZE_MAX

* tests/misc/head-c.sh: When eliding N bytes from a non-seekable
input, N must be slightly smaller than SIZE_MAX in order to handle
input longer than N bytes, since the current implementation buffers
N bytes in memory.  This command would fail on 32-bit systems,
where SIZE_MAX < 1E:
  head --bytes=-E < /dev/null
Instead of "E", use a value slightly smaller than SIZE_MAX.
---
 tests/misc/head-c.sh | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/tests/misc/head-c.sh b/tests/misc/head-c.sh
index 37a86ce..70a6ccc 100755
--- a/tests/misc/head-c.sh
+++ b/tests/misc/head-c.sh
@@ -19,6 +19,7 @@
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ head
 require_ulimit_v_
+getlimits_

 # exercise the fix of 2001-08-18, based on test case from Ian Bruce
 echo abc > in || framework_failure_
@@ -28,9 +29,16 @@ case "$(cat out)" in
   *) fail=1 ;;
 esac

+# Use a limit of N = SIZE_MAX - max_BUFSIZ
+# The "- max_BUFSIZ" term is because head must be able to add BUFSIZ
+# to the selected value of N without exceeding SIZE_MAX.
+# Since we've seen BUFSIZ up to 128K, use 256K to be safe.
+max_BUFSIZ=$(expr 256 '*' 1024)
+lim=$(expr $SIZE_MAX - $max_BUFSIZ)
+
 # Only allocate memory as needed.
 # Coreutils <= 8.21 would allocate memory up front
 # based on the value passed to -c
-(ulimit -v 20000; head --bytes=-E < /dev/null) || fail=1
+(ulimit -v 20000; head --bytes=-$lim < /dev/null) || fail=1

 Exit $fail
--
1.8.3