|
Packit |
6c4009 |
/* fgets with ERANGE error reporting and size_t buffer length.
|
|
Packit |
6c4009 |
Copyright (C) 2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <assert.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include "libioP.h"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Return -1 and set errno to EINVAL if it is ERANGE. */
|
|
Packit |
6c4009 |
static ssize_t
|
|
Packit |
6c4009 |
fail_no_erange (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (errno == ERANGE)
|
|
Packit |
6c4009 |
__set_errno (EINVAL);
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Slow path for reading the line. Called with no data in the stream
|
|
Packit |
6c4009 |
read buffer. Write data to [BUFFER, BUFFER_END). */
|
|
Packit |
6c4009 |
static ssize_t
|
|
Packit |
6c4009 |
readline_slow (FILE *fp, char *buffer, char *buffer_end)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *start = buffer;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
while (buffer < buffer_end)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (__underflow (fp) == EOF)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (_IO_ferror_unlocked (fp))
|
|
Packit |
6c4009 |
/* If the EOF was caused by a read error, report it. */
|
|
Packit |
6c4009 |
return fail_no_erange ();
|
|
Packit |
6c4009 |
*buffer = '\0';
|
|
Packit |
6c4009 |
/* Do not include the null terminator. */
|
|
Packit |
6c4009 |
return buffer - start;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* __underflow has filled the buffer. */
|
|
Packit |
6c4009 |
char *readptr = fp->_IO_read_ptr;
|
|
Packit |
6c4009 |
ssize_t readlen = fp->_IO_read_end - readptr;
|
|
Packit |
6c4009 |
/* Make sure that __underflow really has acquired some data. */
|
|
Packit |
6c4009 |
assert (readlen > 0);
|
|
Packit |
6c4009 |
char *pnl = memchr (readptr, '\n', readlen);
|
|
Packit |
6c4009 |
if (pnl != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* We found the terminator. */
|
|
Packit |
6c4009 |
size_t line_length = pnl - readptr;
|
|
Packit |
6c4009 |
if (line_length + 2 > buffer_end - buffer)
|
|
Packit |
6c4009 |
/* Not enough room in the caller-supplied buffer. */
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
memcpy (buffer, readptr, line_length + 1);
|
|
Packit |
6c4009 |
buffer[line_length + 1] = '\0';
|
|
Packit |
6c4009 |
fp->_IO_read_ptr = pnl + 1;
|
|
Packit |
6c4009 |
/* Do not include the null terminator. */
|
|
Packit |
6c4009 |
return buffer - start + line_length + 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (readlen >= buffer_end - buffer)
|
|
Packit |
6c4009 |
/* Not enough room in the caller-supplied buffer. */
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Save and consume the stream buffer. */
|
|
Packit |
6c4009 |
memcpy (buffer, readptr, readlen);
|
|
Packit |
6c4009 |
fp->_IO_read_ptr = fp->_IO_read_end;
|
|
Packit |
6c4009 |
buffer += readlen;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The line does not fit into the buffer. */
|
|
Packit |
6c4009 |
__set_errno (ERANGE);
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ssize_t
|
|
Packit |
6c4009 |
__libc_readline_unlocked (FILE *fp, char *buffer, size_t buffer_length)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *buffer_end = buffer + buffer_length;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Orient the stream. */
|
|
Packit |
6c4009 |
if (__builtin_expect (fp->_mode, -1) == 0)
|
|
Packit |
6c4009 |
_IO_fwide (fp, -1);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Fast path: The line terminator is found in the buffer. */
|
|
Packit |
6c4009 |
char *readptr = fp->_IO_read_ptr;
|
|
Packit |
6c4009 |
ssize_t readlen = fp->_IO_read_end - readptr;
|
|
Packit |
6c4009 |
off64_t start_offset; /* File offset before reading anything. */
|
|
Packit |
6c4009 |
if (readlen > 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *pnl = memchr (readptr, '\n', readlen);
|
|
Packit |
6c4009 |
if (pnl != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t line_length = pnl - readptr;
|
|
Packit |
6c4009 |
/* Account for line and null terminators. */
|
|
Packit |
6c4009 |
if (line_length + 2 > buffer_length)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
__set_errno (ERANGE);
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
memcpy (buffer, readptr, line_length + 1);
|
|
Packit |
6c4009 |
buffer[line_length + 1] = '\0';
|
|
Packit |
6c4009 |
/* Consume the entire line. */
|
|
Packit |
6c4009 |
fp->_IO_read_ptr = pnl + 1;
|
|
Packit |
6c4009 |
return line_length + 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* If the buffer does not have enough space for what is pending
|
|
Packit |
6c4009 |
in the stream (plus a NUL terminator), the buffer is too
|
|
Packit |
6c4009 |
small. */
|
|
Packit |
6c4009 |
if (readlen + 1 > buffer_length)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
__set_errno (ERANGE);
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* End of line not found. We need all the buffered data. Fall
|
|
Packit |
6c4009 |
through to the slow path. */
|
|
Packit |
6c4009 |
memcpy (buffer, readptr, readlen);
|
|
Packit |
6c4009 |
buffer += readlen;
|
|
Packit |
6c4009 |
/* The original length is invalid after this point. Use
|
|
Packit |
6c4009 |
buffer_end instead. */
|
|
Packit |
6c4009 |
#pragma GCC poison buffer_length
|
|
Packit |
6c4009 |
/* Read the old offset before updating the read pointer. */
|
|
Packit |
6c4009 |
start_offset = __ftello64 (fp);
|
|
Packit |
6c4009 |
fp->_IO_read_ptr = fp->_IO_read_end;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
readlen = 0;
|
|
Packit |
6c4009 |
start_offset = __ftello64 (fp);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Slow path: Read more data from the underlying file. We need to
|
|
Packit |
6c4009 |
restore the file pointer if the buffer is too small. First,
|
|
Packit |
6c4009 |
check if the __ftello64 call above failed. */
|
|
Packit |
6c4009 |
if (start_offset < 0)
|
|
Packit |
6c4009 |
return fail_no_erange ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ssize_t result = readline_slow (fp, buffer, buffer_end);
|
|
Packit |
6c4009 |
if (result < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (errno == ERANGE)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Restore the file pointer so that the caller may read the
|
|
Packit |
6c4009 |
same line again. */
|
|
Packit |
6c4009 |
if (__fseeko64 (fp, start_offset, SEEK_SET) < 0)
|
|
Packit |
6c4009 |
return fail_no_erange ();
|
|
Packit |
6c4009 |
__set_errno (ERANGE);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
/* Do not restore the file position on other errors; it is
|
|
Packit |
6c4009 |
likely that the __fseeko64 call would fail, too. */
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return readlen + result;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
libc_hidden_def (__libc_readline_unlocked)
|