This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[RFC/PoC 1/4] getdelim: Compute result separately
- From: Rasmus Villemoes <rv at rasmusvillemoes dot dk>
- To: libc-alpha at sourceware dot org
- Cc: Jeff King <peff at peff dot net>, Rasmus Villemoes <rv at rasmusvillemoes dot dk>
- Date: Mon, 13 Apr 2015 14:28:34 +0200
- Subject: [RFC/PoC 1/4] getdelim: Compute result separately
- Authentication-results: sourceware.org; auth=none
- References: <1428928117-8643-1-git-send-email-rv at rasmusvillemoes dot dk>
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 --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);
--
2.1.3