xref: /freebsd/lib/libc/stdio/getdelim.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
169099ba2SDavid Schultz /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
469099ba2SDavid Schultz  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
58f8a7947SBryan 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 "namespace.h"
3169099ba2SDavid Schultz #include <sys/param.h>
3269099ba2SDavid Schultz #include <errno.h>
3369099ba2SDavid Schultz #include <limits.h>
3469099ba2SDavid Schultz #include <stdio.h>
3569099ba2SDavid Schultz #include <stdlib.h>
3669099ba2SDavid Schultz #include <string.h>
3769099ba2SDavid Schultz #include "un-namespace.h"
3869099ba2SDavid Schultz 
3969099ba2SDavid Schultz #include "libc_private.h"
4069099ba2SDavid Schultz #include "local.h"
4169099ba2SDavid Schultz 
4269099ba2SDavid Schultz static inline size_t
p2roundup(size_t n)4369099ba2SDavid Schultz p2roundup(size_t n)
4469099ba2SDavid Schultz {
4569099ba2SDavid Schultz 
4669099ba2SDavid Schultz 	if (!powerof2(n)) {
4769099ba2SDavid Schultz 		n--;
4869099ba2SDavid Schultz 		n |= n >> 1;
4969099ba2SDavid Schultz 		n |= n >> 2;
5069099ba2SDavid Schultz 		n |= n >> 4;
5169099ba2SDavid Schultz 		n |= n >> 8;
5269099ba2SDavid Schultz 		n |= n >> 16;
5369099ba2SDavid Schultz #if SIZE_T_MAX > 0xffffffffU
5469099ba2SDavid Schultz 		n |= n >> 32;
5569099ba2SDavid Schultz #endif
5669099ba2SDavid Schultz 		n++;
5769099ba2SDavid Schultz 	}
5869099ba2SDavid Schultz 	return (n);
5969099ba2SDavid Schultz }
6069099ba2SDavid Schultz 
6169099ba2SDavid Schultz /*
6269099ba2SDavid Schultz  * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
6369099ba2SDavid Schultz  */
6469099ba2SDavid Schultz static inline int
expandtofit(char ** __restrict linep,size_t len,size_t * __restrict capp)6569099ba2SDavid Schultz expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
6669099ba2SDavid Schultz {
6769099ba2SDavid Schultz 	char *newline;
6869099ba2SDavid Schultz 	size_t newcap;
6969099ba2SDavid Schultz 
7069099ba2SDavid Schultz 	if (len > (size_t)SSIZE_MAX + 1) {
7169099ba2SDavid Schultz 		errno = EOVERFLOW;
7269099ba2SDavid Schultz 		return (-1);
7369099ba2SDavid Schultz 	}
7469099ba2SDavid Schultz 	if (len > *capp) {
7569099ba2SDavid Schultz 		if (len == (size_t)SSIZE_MAX + 1)	/* avoid overflow */
7669099ba2SDavid Schultz 			newcap = (size_t)SSIZE_MAX + 1;
7769099ba2SDavid Schultz 		else
7869099ba2SDavid Schultz 			newcap = p2roundup(len);
7969099ba2SDavid Schultz 		newline = realloc(*linep, newcap);
8069099ba2SDavid Schultz 		if (newline == NULL)
8169099ba2SDavid Schultz 			return (-1);
8269099ba2SDavid Schultz 		*capp = newcap;
8369099ba2SDavid Schultz 		*linep = newline;
8469099ba2SDavid Schultz 	}
8569099ba2SDavid Schultz 	return (0);
8669099ba2SDavid Schultz }
8769099ba2SDavid Schultz 
8869099ba2SDavid Schultz /*
8969099ba2SDavid Schultz  * Append the src buffer to the *dstp buffer. The buffers are of
9069099ba2SDavid Schultz  * length srclen and *dstlenp, respectively, and dst has space for
9169099ba2SDavid Schultz  * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
9269099ba2SDavid Schultz  * appropriately, and *dstp is reallocated if needed. Returns 0 on
9369099ba2SDavid Schultz  * success, -1 on allocation failure.
9469099ba2SDavid Schultz  */
9569099ba2SDavid Schultz static int
sappend(char ** __restrict dstp,size_t * __restrict dstlenp,size_t * __restrict dstcapp,char * __restrict src,size_t srclen)9669099ba2SDavid Schultz sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
9769099ba2SDavid Schultz 	size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
9869099ba2SDavid Schultz {
9969099ba2SDavid Schultz 
10069099ba2SDavid Schultz 	/* ensure room for srclen + dstlen + terminating NUL */
10169099ba2SDavid Schultz 	if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
10269099ba2SDavid Schultz 		return (-1);
10369099ba2SDavid Schultz 	memcpy(*dstp + *dstlenp, src, srclen);
10469099ba2SDavid Schultz 	*dstlenp += srclen;
10569099ba2SDavid Schultz 	return (0);
10669099ba2SDavid Schultz }
10769099ba2SDavid Schultz 
10869099ba2SDavid Schultz ssize_t
getdelim(char ** __restrict linep,size_t * __restrict linecapp,int delim,FILE * __restrict fp)10969099ba2SDavid Schultz getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
11069099ba2SDavid Schultz 	 FILE * __restrict fp)
11169099ba2SDavid Schultz {
11269099ba2SDavid Schultz 	u_char *endp;
11369099ba2SDavid Schultz 	size_t linelen;
11469099ba2SDavid Schultz 
115fda0a14fSKonstantin Belousov 	FLOCKFILE_CANCELSAFE(fp);
11669099ba2SDavid Schultz 	ORIENT(fp, -1);
11769099ba2SDavid Schultz 
11869099ba2SDavid Schultz 	if (linep == NULL || linecapp == NULL) {
11969099ba2SDavid Schultz 		errno = EINVAL;
12069099ba2SDavid Schultz 		goto error;
12169099ba2SDavid Schultz 	}
12269099ba2SDavid Schultz 
1237e817e2aSDavid Schultz 	if (*linep == NULL)
1247e817e2aSDavid Schultz 		*linecapp = 0;
12569099ba2SDavid Schultz 
12669099ba2SDavid Schultz 	if (fp->_r <= 0 && __srefill(fp)) {
12769099ba2SDavid Schultz 		/* If fp is at EOF already, we just need space for the NUL. */
1281bf6c5f1SAndrey A. Chernov 		if (!__sfeof(fp) || expandtofit(linep, 1, linecapp))
12969099ba2SDavid Schultz 			goto error;
1306685ac34SDavid Schultz 		(*linep)[0] = '\0';
131fda0a14fSKonstantin Belousov 		linelen = -1;
132fda0a14fSKonstantin Belousov 		goto end;
13369099ba2SDavid Schultz 	}
13469099ba2SDavid Schultz 
1356685ac34SDavid Schultz 	linelen = 0;
13669099ba2SDavid Schultz 	while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
13769099ba2SDavid Schultz 		if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r))
13869099ba2SDavid Schultz 			goto error;
1398f8a7947SBryan Drewery 		errno = 0;
14069099ba2SDavid Schultz 		if (__srefill(fp)) {
1418f8a7947SBryan Drewery 			if (__sfeof(fp))
1428f8a7947SBryan Drewery 				goto done;
1438f8a7947SBryan Drewery 			if (errno == EAGAIN) {
1448f8a7947SBryan Drewery 				/*
1458f8a7947SBryan Drewery 				 * We need to undo a partial read that has
1468f8a7947SBryan Drewery 				 * been placed into linep or we would otherwise
1478f8a7947SBryan Drewery 				 * lose it on the next read.
1488f8a7947SBryan Drewery 				 */
1498f8a7947SBryan Drewery 				while (linelen > 0) {
1508f8a7947SBryan Drewery 					if (__ungetc((*linep)[--linelen],
1518f8a7947SBryan Drewery 					    fp) == EOF)
15269099ba2SDavid Schultz 						goto error;
1538f8a7947SBryan Drewery 				}
1548f8a7947SBryan Drewery 				/*
1558f8a7947SBryan Drewery 				 * This is not strictly needed but it is
1568f8a7947SBryan Drewery 				 * possible a consumer has worked around an
1578f8a7947SBryan Drewery 				 * older EAGAIN bug by buffering a partial
1588f8a7947SBryan Drewery 				 * return.
1598f8a7947SBryan Drewery 				 */
1608f8a7947SBryan Drewery 				(*linep)[0] = '\0';
1618f8a7947SBryan Drewery 			}
1628f8a7947SBryan Drewery 			goto error;
16369099ba2SDavid Schultz 		}
16469099ba2SDavid Schultz 	}
16569099ba2SDavid Schultz 	endp++;	/* snarf the delimiter, too */
16669099ba2SDavid Schultz 	if (sappend(linep, &linelen, linecapp, fp->_p, endp - fp->_p))
16769099ba2SDavid Schultz 		goto error;
16869099ba2SDavid Schultz 	fp->_r -= endp - fp->_p;
16969099ba2SDavid Schultz 	fp->_p = endp;
17069099ba2SDavid Schultz done:
17169099ba2SDavid Schultz 	/* Invariant: *linep has space for at least linelen+1 bytes. */
17269099ba2SDavid Schultz 	(*linep)[linelen] = '\0';
173fda0a14fSKonstantin Belousov end:
174fda0a14fSKonstantin Belousov 	FUNLOCKFILE_CANCELSAFE();
17569099ba2SDavid Schultz 	return (linelen);
17669099ba2SDavid Schultz 
17769099ba2SDavid Schultz error:
17869099ba2SDavid Schultz 	fp->_flags |= __SERR;
179fda0a14fSKonstantin Belousov 	linelen = -1;
180fda0a14fSKonstantin Belousov 	goto end;
18169099ba2SDavid Schultz }
182