(getndelim2): When size calculation overflows,
[gnulib.git] / lib / getndelim2.c
1 /* getndelim2 - Read a line from a stream, stopping at one of 2 delimiters,
2    with bounded memory allocation.
3
4    Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003 Free Software
5    Foundation, Inc.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* Originally written by Jan Brittenson, bson@gnu.ai.mit.edu.  */
22
23 #if HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 /* Specification.  */
28 #include "getndelim2.h"
29
30 #include <stdlib.h>
31
32 #include "unlocked-io.h"
33
34 /* Always add at least this many bytes when extending the buffer.  */
35 #define MIN_CHUNK 64
36
37 ssize_t
38 getndelim2 (char **lineptr, size_t *linesize, size_t nmax,
39             FILE *stream, int delim1, int delim2, size_t offset)
40 {
41   size_t nbytes_avail;          /* Allocated but unused chars in *LINEPTR.  */
42   char *read_pos;               /* Where we're reading into *LINEPTR. */
43
44   if (!lineptr || !linesize || !nmax || !stream)
45     return -1;
46
47   if (!*lineptr)
48     {
49       size_t newlinesize = MIN_CHUNK;
50
51       if (newlinesize > nmax)
52         newlinesize = nmax;
53
54       *linesize = newlinesize;
55       *lineptr = malloc (*linesize);
56       if (!*lineptr)
57         return -1;
58     }
59
60   if (*linesize < offset)
61     return -1;
62
63   nbytes_avail = *linesize - offset;
64   read_pos = *lineptr + offset;
65
66   if (nbytes_avail == 0 && *linesize >= nmax)
67     return -1;
68
69   for (;;)
70     {
71       /* Here always *lineptr + *linesize == read_pos + nbytes_avail.  */
72
73       register int c;
74
75       /* We always want at least one char left in the buffer, since we
76          always (unless we get an error while reading the first char)
77          NUL-terminate the line buffer.  */
78
79       if (nbytes_avail < 2 && *linesize < nmax)
80         {
81           size_t newlinesize =
82             (*linesize > MIN_CHUNK ? 2 * *linesize : *linesize + MIN_CHUNK);
83
84           if (! (*linesize < newlinesize && newlinesize <= nmax))
85             newlinesize = nmax;
86
87           *linesize = newlinesize;
88           nbytes_avail = *linesize + *lineptr - read_pos;
89           *lineptr = realloc (*lineptr, *linesize);
90           if (!*lineptr)
91             return -1;
92           read_pos = *linesize - nbytes_avail + *lineptr;
93         }
94
95       c = getc (stream);
96       if (c == EOF)
97         {
98           /* Return partial line, if any.  */
99           if (read_pos == *lineptr)
100             return -1;
101           else
102             break;
103         }
104
105       if (nbytes_avail >= 2)
106         {
107           *read_pos++ = c;
108           nbytes_avail--;
109         }
110
111       if (c == delim1 || (delim2 && c == delim2))
112         /* Return the line.  */
113         break;
114     }
115
116   /* Done - NUL terminate and return the number of chars read.
117      At this point we know that nbytes_avail >= 1.  */
118   *read_pos = '\0';
119
120   return read_pos - (*lineptr + offset);
121 }