diff mbox

[RFC/PoC,1/4] getdelim: Compute result separately

Message ID 1428928117-8643-2-git-send-email-rv@rasmusvillemoes.dk
State New
Headers show

Commit Message

Rasmus Villemoes April 13, 2015, 12:28 p.m. UTC
In preparation for introducing getdelim_append (which will essentially
change cur_len from a local variable to an input/output parameter),
compute result separately from cur_len.

Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
---
 libio/iogetdelim.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/libio/iogetdelim.c b/libio/iogetdelim.c
index ac93044..6606c4b 100644
--- a/libio/iogetdelim.c
+++ b/libio/iogetdelim.c
@@ -43,7 +43,7 @@  _IO_getdelim (lineptr, n, delimiter, fp)
      int delimiter;
      _IO_FILE *fp;
 {
-  _IO_ssize_t result;
+  _IO_ssize_t result = 0;
   _IO_ssize_t cur_len = 0;
   _IO_ssize_t len;
 
@@ -89,7 +89,8 @@  _IO_getdelim (lineptr, n, delimiter, fp)
       t = (char *) memchr ((void *) fp->_IO_read_ptr, delimiter, len);
       if (t != NULL)
 	len = (t - fp->_IO_read_ptr) + 1;
-      if (__glibc_unlikely (len >= SSIZE_MAX - cur_len))
+      if (__glibc_unlikely (len >= SSIZE_MAX - cur_len) ||
+	  __glibc_unlikely (len >= SSIZE_MAX - result))
 	{
 	  __set_errno (EOVERFLOW);
 	  result = -1;
@@ -115,12 +116,12 @@  _IO_getdelim (lineptr, n, delimiter, fp)
       memcpy (*lineptr + cur_len, (void *) fp->_IO_read_ptr, len);
       fp->_IO_read_ptr += len;
       cur_len += len;
+      result += len;
       if (t != NULL || __underflow (fp) == EOF)
 	break;
       len = fp->_IO_read_end - fp->_IO_read_ptr;
     }
   (*lineptr)[cur_len] = '\0';
-  result = cur_len;
 
 unlock_return:
   _IO_release_lock (fp);