changeset 8077:9cd2d21f40cc

* lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow checking code. Set N = ceil (1.5 * N) rather than to a slightly larger value. Give tools a better chance to allocate space for very large buffers. * lib/xalloc.h (x2nrealloc): Use 3/2, not 2, as buffer size factor.
author Paul Eggert <eggert@cs.ucla.edu>
date Sun, 04 Feb 2007 02:20:40 +0000
parents 662352a3327e
children 0a080a2d054b
files ChangeLog lib/xalloc.h
diffstat 2 files changed, 16 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
+2007-02-03  Paul Eggert  <eggert@cs.ucla.edu>
+
+	* lib/xalloc.h (x2nrealloc): Fix an unlikely bug in the overflow
+	checking code.  Set N = ceil (1.5 * N) rather than to a slightly
+	larger value.
+
 2007-02-03  Jim Meyering  <jim@meyering.net>
 
+        Give tools a better chance to allocate space for very large buffers.
+	* lib/xalloc.h (x2nrealloc): Use 3/2, not 2, as buffer size factor.
+
 	Make pwd and readlink work also when run with an unreadable parent dir
 	on systems with openat support.
 	* lib/getcwd.c (__getcwd) [HAVE_PARTLY_WORKING_GETCWD]: Use the system
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -141,7 +141,7 @@
 
    In the following implementation, nonzero sizes are increased by a
    factor of approximately 1.5 so that repeated reallocations have
-   O(N log N) overall cost rather than O(N**2) cost, but the
+   O(N) overall cost rather than O(N**2) cost, but the
    specification for this function does not guarantee that rate.
 
    Here is an example of use:
@@ -204,9 +204,13 @@
     }
   else
     {
-      if ((2 * (((size_t) -1 - 1) / 3)) / s < n)
+      /* Set N = ceil (1.5 * N) so that progress is made if N == 1.
+	 Check for overflow, so that N * S stays in size_t range.
+	 The check is slightly conservative, but an exact check isn't
+	 worth the trouble.  */
+      if ((size_t) -1 / 3 * 2 / s <= n)
 	xalloc_die ();
-      n = n + n / 2 + 1;
+      n += (n + 1) / 2;
     }
 
   *pn = n;