coreutils
[Top][All Lists]
Advanced

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

Re: bug#13530: head: memory exhausted when printing all from stdin but l


From: Jim Meyering
Subject: Re: bug#13530: head: memory exhausted when printing all from stdin but last P/E bytes
Date: Tue, 28 May 2013 03:04:21 +0200

Paul Eggert wrote:

> On 05/27/2013 05:07 PM, Jim Meyering wrote:
>
>> +max_BUFSIZ=$(expr 256 '*' 1024)
>> +lim=$(expr $SIZE_MAX - $max_BUFSIZ)
>
> Can't this code fail, due to overflow, on non-GMP hosts?  See:
>
> http://lists.gnu.org/archive/html/coreutils/2013-05/msg00060.html
>
> and look for "$SIZE_MAX".

Here are two patches.
The first factors out the definition into a new function.
The second uses it in the revised head-c-fixing patch.
Both tests still pass, though I haven't yet run them
against a GMP-free expr.

>From f97095c19244d61af4172ab457b5bc79081ada79 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Mon, 27 May 2013 17:59:41 -0700
Subject: [PATCH 1/2] maint: factor out new subtract_one_ function

* tests/misc/cut-huge-range.sh (subtract_one): Move definition
of this sed script to init.cfg so we can use it from another test.
* init.cfg (subtract_one_): New function, from that variable.
---
 init.cfg                     | 24 ++++++++++++++++++++++++
 tests/misc/cut-huge-range.sh | 22 +---------------------
 2 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/init.cfg b/init.cfg
index c48607c..b013c4e 100644
--- a/init.cfg
+++ b/init.cfg
@@ -596,4 +596,28 @@ require_gnu_()
     || skip_ 'not running on GNU/Hurd'
 }

+subtract_one_()
+{
+  # sed script to subtract one from the input.
+  # Each input line should consist of a positive decimal number.
+  # Each output line's number is one less than the input's.
+  # There is no limit (other than line length) on the number's magnitude.
+  local subtract_one='
+    s/$/@/
+    : again
+    s/0@/@9/
+    s/1@/0/
+    s/2@/1/
+    s/3@/2/
+    s/4@/3/
+    s/5@/4/
+    s/6@/5/
+    s/7@/6/
+    s/8@/7/
+    s/9@/8/
+    t again
+  '
+  sed "$subtract_one"
+}
+
 sanitize_path_
diff --git a/tests/misc/cut-huge-range.sh b/tests/misc/cut-huge-range.sh
index 7816577..ae7cc70 100755
--- a/tests/misc/cut-huge-range.sh
+++ b/tests/misc/cut-huge-range.sh
@@ -21,31 +21,11 @@ print_ver_ cut
 require_ulimit_v_
 getlimits_

-# sed script to subtract one from the input.
-# Each input line should consist of a positive decimal number.
-# Each output line's number is one less than the input's.
-# There's no limit (other than line length) on the number's magnitude.
-subtract_one='
-  s/$/@/
-  : again
-  s/0@/@9/
-  s/1@/0/
-  s/2@/1/
-  s/3@/2/
-  s/4@/3/
-  s/5@/4/
-  s/6@/5/
-  s/7@/6/
-  s/8@/7/
-  s/9@/8/
-  t again
-'
-
 # Ensure we can cut up to our sentinel value.
 # This is currently SIZE_MAX, but could be raised to UINTMAX_MAX
 # if we didn't allocate memory for each line as a unit.
 # Don't use expr to subtract one, since SIZE_MAX may exceed its maximum value.
-CUT_MAX=$(echo $SIZE_MAX | sed "$subtract_one")
+CUT_MAX=$(echo $SIZE_MAX | subtract_one_ )

 # From coreutils-8.10 through 8.20, this would make cut try to allocate
 # a 256MiB bit vector.  With a 20MB limit on VM, the following would fail.
--
1.8.3


>From 905cd2b5c503c82894b433767810ff2f1e40b69d Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Mon, 27 May 2013 17:01:14 -0700
Subject: [PATCH 2/2] 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 | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/tests/misc/head-c.sh b/tests/misc/head-c.sh
index 37a86ce..155c7f6 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,24 @@ 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)
+
+# Normally we would just write this,
+#  lim=$(expr $SIZE_MAX - $max_BUFSIZ)
+# But that fails for non-GMP expr.  See this:
+# https://lists.gnu.org/archive/html/coreutils/2013-05/msg00060.html
+# Instead, use that same approach to obtain SIZE_MAX-1, and *then*
+# subtract $max_BUFSIZ.
+lim=$(echo $SIZE_MAX | subtract_one_)
+lim=$(expr $lim - $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



reply via email to

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