summaryrefslogtreecommitdiff
path: root/libio/readline.c
blob: 3f2a9a43b9bbfa36226a2854badfc215db0b73fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* fgets with ERANGE error reporting and size_t buffer length.
   Copyright (C) 2018-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "libioP.h"

/* Return -1 and set errno to EINVAL if it is ERANGE.  */
static ssize_t
fail_no_erange (void)
{
  if (errno == ERANGE)
    __set_errno (EINVAL);
  return -1;
}

/* Slow path for reading the line.  Called with no data in the stream
   read buffer.  Write data to [BUFFER, BUFFER_END).  */
static ssize_t
readline_slow (FILE *fp, char *buffer, char *buffer_end)
{
  char *start = buffer;

  while (buffer < buffer_end)
    {
      if (__underflow (fp) == EOF)
        {
          if (_IO_ferror_unlocked (fp))
            /* If the EOF was caused by a read error, report it.  */
            return fail_no_erange ();
          *buffer = '\0';
          /* Do not include the null terminator.  */
          return buffer - start;
        }

      /* __underflow has filled the buffer.  */
      char *readptr = fp->_IO_read_ptr;
      ssize_t readlen = fp->_IO_read_end - readptr;
      /* Make sure that __underflow really has acquired some data.  */
      assert (readlen > 0);
      char *pnl = memchr (readptr, '\n', readlen);
      if (pnl != NULL)
        {
          /* We found the terminator.  */
          size_t line_length = pnl - readptr;
          if (line_length + 2 > buffer_end - buffer)
            /* Not enough room in the caller-supplied buffer.  */
            break;
          memcpy (buffer, readptr, line_length + 1);
          buffer[line_length + 1] = '\0';
          fp->_IO_read_ptr = pnl + 1;
          /* Do not include the null terminator.  */
          return buffer - start + line_length + 1;
        }

      if (readlen >= buffer_end - buffer)
        /* Not enough room in the caller-supplied buffer.  */
        break;

      /* Save and consume the stream buffer.  */
      memcpy (buffer, readptr, readlen);
      fp->_IO_read_ptr = fp->_IO_read_end;
      buffer += readlen;
    }

  /* The line does not fit into the buffer.  */
  __set_errno (ERANGE);
  return -1;
}

ssize_t
__libc_readline_unlocked (FILE *fp, char *buffer, size_t buffer_length)
{
  char *buffer_end = buffer + buffer_length;

  /* Orient the stream.  */
  if (__builtin_expect (fp->_mode, -1) == 0)
    _IO_fwide (fp, -1);

  /* Fast path: The line terminator is found in the buffer.  */
  char *readptr = fp->_IO_read_ptr;
  ssize_t readlen = fp->_IO_read_end - readptr;
  off64_t start_offset;         /* File offset before reading anything.  */
  if (readlen > 0)
    {
      char *pnl = memchr (readptr, '\n', readlen);
      if (pnl != NULL)
        {
          size_t line_length = pnl - readptr;
          /* Account for line and null terminators.  */
          if (line_length + 2 > buffer_length)
            {
              __set_errno (ERANGE);
              return -1;
            }
          memcpy (buffer, readptr, line_length + 1);
          buffer[line_length + 1] = '\0';
          /* Consume the entire line.  */
          fp->_IO_read_ptr = pnl + 1;
          return line_length + 1;
        }

      /* If the buffer does not have enough space for what is pending
         in the stream (plus a NUL terminator), the buffer is too
         small.  */
      if (readlen + 1 > buffer_length)
        {
          __set_errno (ERANGE);
          return -1;
        }

      /* End of line not found.  We need all the buffered data.  Fall
         through to the slow path.  */
      memcpy (buffer, readptr, readlen);
      buffer += readlen;
      /* The original length is invalid after this point.  Use
         buffer_end instead.  */
#pragma GCC poison buffer_length
      /* Read the old offset before updating the read pointer.  */
      start_offset = __ftello64 (fp);
      fp->_IO_read_ptr = fp->_IO_read_end;
    }
  else
    {
      readlen = 0;
      start_offset = __ftello64 (fp);
    }

  /* Slow path: Read more data from the underlying file.  We need to
     restore the file pointer if the buffer is too small.  First,
     check if the __ftello64 call above failed.  */
  if (start_offset < 0)
    return fail_no_erange ();

  ssize_t result = readline_slow (fp, buffer, buffer_end);
  if (result < 0)
    {
      if (errno == ERANGE)
        {
          /* Restore the file pointer so that the caller may read the
             same line again.  */
          if (__fseeko64 (fp, start_offset, SEEK_SET) < 0)
            return fail_no_erange ();
          __set_errno (ERANGE);
        }
      /* Do not restore the file position on other errors; it is
         likely that the __fseeko64 call would fail, too.  */
      return -1;
    }
  return readlen + result;
}
libc_hidden_def (__libc_readline_unlocked)