changeset 4806:151b8ba4ab1d

(getndelim2): When size calculation overflows, ceiling the allocation at NMAX bytes.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 22 Oct 2003 05:53:05 +0000
parents 8f430f14ff21
children d76c27f639f9
files lib/ChangeLog lib/getndelim2.c
diffstat 2 files changed, 12 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,10 @@
 2003-10-21  Paul Eggert  <eggert@twinsun.com>
 
+	* getndelim2.c (getndelim2): When size calculation overflows,
+	ceiling the allocation at NMAX bytes rather than silently
+	discarding input bytes before NMAX is reached.  This makes
+	a difference only if NMAX exceeds SIZE_MAX / 2.
+
 	* obstack.c: Merge from glibc.
 	[defined _LIBC]: Include <obstack.h>, not "obstack.h".
 	Add libc_hidden_def (_obstack_newchunk).
--- a/lib/getndelim2.c
+++ b/lib/getndelim2.c
@@ -81,18 +81,15 @@
 	  size_t newlinesize =
 	    (*linesize > MIN_CHUNK ? 2 * *linesize : *linesize + MIN_CHUNK);
 
-	  if (newlinesize > nmax)
+	  if (! (*linesize < newlinesize && newlinesize <= nmax))
 	    newlinesize = nmax;
 
-	  if (newlinesize > *linesize)
-	    {
-	      *linesize = newlinesize;
-	      nbytes_avail = *linesize + *lineptr - read_pos;
-	      *lineptr = realloc (*lineptr, *linesize);
-	      if (!*lineptr)
-		return -1;
-	      read_pos = *linesize - nbytes_avail + *lineptr;
-	    }
+	  *linesize = newlinesize;
+	  nbytes_avail = *linesize + *lineptr - read_pos;
+	  *lineptr = realloc (*lineptr, *linesize);
+	  if (!*lineptr)
+	    return -1;
+	  read_pos = *linesize - nbytes_avail + *lineptr;
 	}
 
       c = getc (stream);