(getndelim2): When size calculation overflows,
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 22 Oct 2003 05:53:05 +0000 (05:53 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 22 Oct 2003 05:53:05 +0000 (05:53 +0000)
ceiling the allocation at NMAX bytes.

lib/ChangeLog
lib/getndelim2.c

index ecc0c9693c0d8bd841361219d4233aa8b9ea3ea9..00531bec0e5ec15ce249d3b0e6bd59fb9635714b 100644 (file)
@@ -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).
index 6f0868999525c671560ad97759abc0dec2de4cff..3959462593d33460b35b072dcaca161d21c7d2e2 100644 (file)
@@ -81,18 +81,15 @@ getndelim2 (char **lineptr, size_t *linesize, size_t nmax,
          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);