xref: /freebsd/lib/libc/stdio/getdelim.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
5  * Copyright (c) 2021 Dell EMC
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "namespace.h"
34 #include <sys/param.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include "un-namespace.h"
41 
42 #include "libc_private.h"
43 #include "local.h"
44 
45 static inline size_t
46 p2roundup(size_t n)
47 {
48 
49 	if (!powerof2(n)) {
50 		n--;
51 		n |= n >> 1;
52 		n |= n >> 2;
53 		n |= n >> 4;
54 		n |= n >> 8;
55 		n |= n >> 16;
56 #if SIZE_T_MAX > 0xffffffffU
57 		n |= n >> 32;
58 #endif
59 		n++;
60 	}
61 	return (n);
62 }
63 
64 /*
65  * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
66  */
67 static inline int
68 expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
69 {
70 	char *newline;
71 	size_t newcap;
72 
73 	if (len > (size_t)SSIZE_MAX + 1) {
74 		errno = EOVERFLOW;
75 		return (-1);
76 	}
77 	if (len > *capp) {
78 		if (len == (size_t)SSIZE_MAX + 1)	/* avoid overflow */
79 			newcap = (size_t)SSIZE_MAX + 1;
80 		else
81 			newcap = p2roundup(len);
82 		newline = realloc(*linep, newcap);
83 		if (newline == NULL)
84 			return (-1);
85 		*capp = newcap;
86 		*linep = newline;
87 	}
88 	return (0);
89 }
90 
91 /*
92  * Append the src buffer to the *dstp buffer. The buffers are of
93  * length srclen and *dstlenp, respectively, and dst has space for
94  * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
95  * appropriately, and *dstp is reallocated if needed. Returns 0 on
96  * success, -1 on allocation failure.
97  */
98 static int
99 sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
100 	size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
101 {
102 
103 	/* ensure room for srclen + dstlen + terminating NUL */
104 	if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
105 		return (-1);
106 	memcpy(*dstp + *dstlenp, src, srclen);
107 	*dstlenp += srclen;
108 	return (0);
109 }
110 
111 ssize_t
112 getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
113 	 FILE * __restrict fp)
114 {
115 	u_char *endp;
116 	size_t linelen;
117 
118 	FLOCKFILE_CANCELSAFE(fp);
119 	ORIENT(fp, -1);
120 
121 	if (linep == NULL || linecapp == NULL) {
122 		errno = EINVAL;
123 		goto error;
124 	}
125 
126 	if (*linep == NULL)
127 		*linecapp = 0;
128 
129 	if (fp->_r <= 0 && __srefill(fp)) {
130 		/* If fp is at EOF already, we just need space for the NUL. */
131 		if (!__sfeof(fp) || expandtofit(linep, 1, linecapp))
132 			goto error;
133 		(*linep)[0] = '\0';
134 		linelen = -1;
135 		goto end;
136 	}
137 
138 	linelen = 0;
139 	while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
140 		if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r))
141 			goto error;
142 		errno = 0;
143 		if (__srefill(fp)) {
144 			if (__sfeof(fp))
145 				goto done;
146 			if (errno == EAGAIN) {
147 				/*
148 				 * We need to undo a partial read that has
149 				 * been placed into linep or we would otherwise
150 				 * lose it on the next read.
151 				 */
152 				while (linelen > 0) {
153 					if (__ungetc((*linep)[--linelen],
154 					    fp) == EOF)
155 						goto error;
156 				}
157 				/*
158 				 * This is not strictly needed but it is
159 				 * possible a consumer has worked around an
160 				 * older EAGAIN bug by buffering a partial
161 				 * return.
162 				 */
163 				(*linep)[0] = '\0';
164 			}
165 			goto error;
166 		}
167 	}
168 	endp++;	/* snarf the delimiter, too */
169 	if (sappend(linep, &linelen, linecapp, fp->_p, endp - fp->_p))
170 		goto error;
171 	fp->_r -= endp - fp->_p;
172 	fp->_p = endp;
173 done:
174 	/* Invariant: *linep has space for at least linelen+1 bytes. */
175 	(*linep)[linelen] = '\0';
176 end:
177 	FUNLOCKFILE_CANCELSAFE();
178 	return (linelen);
179 
180 error:
181 	fp->_flags |= __SERR;
182 	linelen = -1;
183 	goto end;
184 }
185