xref: /freebsd/lib/libc/stdio/getdelim.c (revision 8f8a794775bd6da69514d008fe03edb808bbc67d)
169099ba2SDavid Schultz /*-
2d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3d915a14eSPedro F. Giffuni  *
469099ba2SDavid Schultz  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
5*8f8a7947SBryan Drewery  * Copyright (c) 2021 Dell EMC
669099ba2SDavid Schultz  * All rights reserved.
769099ba2SDavid Schultz  *
869099ba2SDavid Schultz  * Redistribution and use in source and binary forms, with or without
969099ba2SDavid Schultz  * modification, are permitted provided that the following conditions
1069099ba2SDavid Schultz  * are met:
1169099ba2SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
1269099ba2SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
1369099ba2SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
1469099ba2SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
1569099ba2SDavid Schultz  *    documentation and/or other materials provided with the distribution.
1669099ba2SDavid Schultz  *
1769099ba2SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1869099ba2SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1969099ba2SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2069099ba2SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2169099ba2SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2269099ba2SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2369099ba2SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2469099ba2SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2569099ba2SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2669099ba2SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2769099ba2SDavid Schultz  * SUCH DAMAGE.
2869099ba2SDavid Schultz  */
2969099ba2SDavid Schultz 
3069099ba2SDavid Schultz #include <sys/cdefs.h>
3169099ba2SDavid Schultz __FBSDID("$FreeBSD$");
3269099ba2SDavid Schultz 
3369099ba2SDavid Schultz #include "namespace.h"
3469099ba2SDavid Schultz #include <sys/param.h>
3569099ba2SDavid Schultz #include <errno.h>
3669099ba2SDavid Schultz #include <limits.h>
3769099ba2SDavid Schultz #include <stdio.h>
3869099ba2SDavid Schultz #include <stdlib.h>
3969099ba2SDavid Schultz #include <string.h>
4069099ba2SDavid Schultz #include "un-namespace.h"
4169099ba2SDavid Schultz 
4269099ba2SDavid Schultz #include "libc_private.h"
4369099ba2SDavid Schultz #include "local.h"
4469099ba2SDavid Schultz 
4569099ba2SDavid Schultz static inline size_t
4669099ba2SDavid Schultz p2roundup(size_t n)
4769099ba2SDavid Schultz {
4869099ba2SDavid Schultz 
4969099ba2SDavid Schultz 	if (!powerof2(n)) {
5069099ba2SDavid Schultz 		n--;
5169099ba2SDavid Schultz 		n |= n >> 1;
5269099ba2SDavid Schultz 		n |= n >> 2;
5369099ba2SDavid Schultz 		n |= n >> 4;
5469099ba2SDavid Schultz 		n |= n >> 8;
5569099ba2SDavid Schultz 		n |= n >> 16;
5669099ba2SDavid Schultz #if SIZE_T_MAX > 0xffffffffU
5769099ba2SDavid Schultz 		n |= n >> 32;
5869099ba2SDavid Schultz #endif
5969099ba2SDavid Schultz 		n++;
6069099ba2SDavid Schultz 	}
6169099ba2SDavid Schultz 	return (n);
6269099ba2SDavid Schultz }
6369099ba2SDavid Schultz 
6469099ba2SDavid Schultz /*
6569099ba2SDavid Schultz  * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
6669099ba2SDavid Schultz  */
6769099ba2SDavid Schultz static inline int
6869099ba2SDavid Schultz expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
6969099ba2SDavid Schultz {
7069099ba2SDavid Schultz 	char *newline;
7169099ba2SDavid Schultz 	size_t newcap;
7269099ba2SDavid Schultz 
7369099ba2SDavid Schultz 	if (len > (size_t)SSIZE_MAX + 1) {
7469099ba2SDavid Schultz 		errno = EOVERFLOW;
7569099ba2SDavid Schultz 		return (-1);
7669099ba2SDavid Schultz 	}
7769099ba2SDavid Schultz 	if (len > *capp) {
7869099ba2SDavid Schultz 		if (len == (size_t)SSIZE_MAX + 1)	/* avoid overflow */
7969099ba2SDavid Schultz 			newcap = (size_t)SSIZE_MAX + 1;
8069099ba2SDavid Schultz 		else
8169099ba2SDavid Schultz 			newcap = p2roundup(len);
8269099ba2SDavid Schultz 		newline = realloc(*linep, newcap);
8369099ba2SDavid Schultz 		if (newline == NULL)
8469099ba2SDavid Schultz 			return (-1);
8569099ba2SDavid Schultz 		*capp = newcap;
8669099ba2SDavid Schultz 		*linep = newline;
8769099ba2SDavid Schultz 	}
8869099ba2SDavid Schultz 	return (0);
8969099ba2SDavid Schultz }
9069099ba2SDavid Schultz 
9169099ba2SDavid Schultz /*
9269099ba2SDavid Schultz  * Append the src buffer to the *dstp buffer. The buffers are of
9369099ba2SDavid Schultz  * length srclen and *dstlenp, respectively, and dst has space for
9469099ba2SDavid Schultz  * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
9569099ba2SDavid Schultz  * appropriately, and *dstp is reallocated if needed. Returns 0 on
9669099ba2SDavid Schultz  * success, -1 on allocation failure.
9769099ba2SDavid Schultz  */
9869099ba2SDavid Schultz static int
9969099ba2SDavid Schultz sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
10069099ba2SDavid Schultz 	size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
10169099ba2SDavid Schultz {
10269099ba2SDavid Schultz 
10369099ba2SDavid Schultz 	/* ensure room for srclen + dstlen + terminating NUL */
10469099ba2SDavid Schultz 	if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
10569099ba2SDavid Schultz 		return (-1);
10669099ba2SDavid Schultz 	memcpy(*dstp + *dstlenp, src, srclen);
10769099ba2SDavid Schultz 	*dstlenp += srclen;
10869099ba2SDavid Schultz 	return (0);
10969099ba2SDavid Schultz }
11069099ba2SDavid Schultz 
11169099ba2SDavid Schultz ssize_t
11269099ba2SDavid Schultz getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
11369099ba2SDavid Schultz 	 FILE * __restrict fp)
11469099ba2SDavid Schultz {
11569099ba2SDavid Schultz 	u_char *endp;
11669099ba2SDavid Schultz 	size_t linelen;
11769099ba2SDavid Schultz 
118fda0a14fSKonstantin Belousov 	FLOCKFILE_CANCELSAFE(fp);
11969099ba2SDavid Schultz 	ORIENT(fp, -1);
12069099ba2SDavid Schultz 
12169099ba2SDavid Schultz 	if (linep == NULL || linecapp == NULL) {
12269099ba2SDavid Schultz 		errno = EINVAL;
12369099ba2SDavid Schultz 		goto error;
12469099ba2SDavid Schultz 	}
12569099ba2SDavid Schultz 
1267e817e2aSDavid Schultz 	if (*linep == NULL)
1277e817e2aSDavid Schultz 		*linecapp = 0;
12869099ba2SDavid Schultz 
12969099ba2SDavid Schultz 	if (fp->_r <= 0 && __srefill(fp)) {
13069099ba2SDavid Schultz 		/* If fp is at EOF already, we just need space for the NUL. */
1311bf6c5f1SAndrey A. Chernov 		if (!__sfeof(fp) || expandtofit(linep, 1, linecapp))
13269099ba2SDavid Schultz 			goto error;
1336685ac34SDavid Schultz 		(*linep)[0] = '\0';
134fda0a14fSKonstantin Belousov 		linelen = -1;
135fda0a14fSKonstantin Belousov 		goto end;
13669099ba2SDavid Schultz 	}
13769099ba2SDavid Schultz 
1386685ac34SDavid Schultz 	linelen = 0;
13969099ba2SDavid Schultz 	while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
14069099ba2SDavid Schultz 		if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r))
14169099ba2SDavid Schultz 			goto error;
142*8f8a7947SBryan Drewery 		errno = 0;
14369099ba2SDavid Schultz 		if (__srefill(fp)) {
144*8f8a7947SBryan Drewery 			if (__sfeof(fp))
145*8f8a7947SBryan Drewery 				goto done;
146*8f8a7947SBryan Drewery 			if (errno == EAGAIN) {
147*8f8a7947SBryan Drewery 				/*
148*8f8a7947SBryan Drewery 				 * We need to undo a partial read that has
149*8f8a7947SBryan Drewery 				 * been placed into linep or we would otherwise
150*8f8a7947SBryan Drewery 				 * lose it on the next read.
151*8f8a7947SBryan Drewery 				 */
152*8f8a7947SBryan Drewery 				while (linelen > 0) {
153*8f8a7947SBryan Drewery 					if (__ungetc((*linep)[--linelen],
154*8f8a7947SBryan Drewery 					    fp) == EOF)
15569099ba2SDavid Schultz 						goto error;
156*8f8a7947SBryan Drewery 				}
157*8f8a7947SBryan Drewery 				/*
158*8f8a7947SBryan Drewery 				 * This is not strictly needed but it is
159*8f8a7947SBryan Drewery 				 * possible a consumer has worked around an
160*8f8a7947SBryan Drewery 				 * older EAGAIN bug by buffering a partial
161*8f8a7947SBryan Drewery 				 * return.
162*8f8a7947SBryan Drewery 				 */
163*8f8a7947SBryan Drewery 				(*linep)[0] = '\0';
164*8f8a7947SBryan Drewery 			}
165*8f8a7947SBryan Drewery 			goto error;
16669099ba2SDavid Schultz 		}
16769099ba2SDavid Schultz 	}
16869099ba2SDavid Schultz 	endp++;	/* snarf the delimiter, too */
16969099ba2SDavid Schultz 	if (sappend(linep, &linelen, linecapp, fp->_p, endp - fp->_p))
17069099ba2SDavid Schultz 		goto error;
17169099ba2SDavid Schultz 	fp->_r -= endp - fp->_p;
17269099ba2SDavid Schultz 	fp->_p = endp;
17369099ba2SDavid Schultz done:
17469099ba2SDavid Schultz 	/* Invariant: *linep has space for at least linelen+1 bytes. */
17569099ba2SDavid Schultz 	(*linep)[linelen] = '\0';
176fda0a14fSKonstantin Belousov end:
177fda0a14fSKonstantin Belousov 	FUNLOCKFILE_CANCELSAFE();
17869099ba2SDavid Schultz 	return (linelen);
17969099ba2SDavid Schultz 
18069099ba2SDavid Schultz error:
18169099ba2SDavid Schultz 	fp->_flags |= __SERR;
182fda0a14fSKonstantin Belousov 	linelen = -1;
183fda0a14fSKonstantin Belousov 	goto end;
18469099ba2SDavid Schultz }
185