xref: /freebsd/lib/libc/stdio/getdelim.c (revision d915a14ef094c8dfc1a5aee70e135abfec01d0f1)
169099ba2SDavid Schultz /*-
2*d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d915a14eSPedro F. Giffuni  *
469099ba2SDavid Schultz  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
569099ba2SDavid Schultz  * All rights reserved.
669099ba2SDavid Schultz  *
769099ba2SDavid Schultz  * Redistribution and use in source and binary forms, with or without
869099ba2SDavid Schultz  * modification, are permitted provided that the following conditions
969099ba2SDavid Schultz  * are met:
1069099ba2SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
1169099ba2SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
1269099ba2SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
1369099ba2SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
1469099ba2SDavid Schultz  *    documentation and/or other materials provided with the distribution.
1569099ba2SDavid Schultz  *
1669099ba2SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1769099ba2SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1869099ba2SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1969099ba2SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2069099ba2SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2169099ba2SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2269099ba2SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2369099ba2SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2469099ba2SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2569099ba2SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2669099ba2SDavid Schultz  * SUCH DAMAGE.
2769099ba2SDavid Schultz  */
2869099ba2SDavid Schultz 
2969099ba2SDavid Schultz #include <sys/cdefs.h>
3069099ba2SDavid Schultz __FBSDID("$FreeBSD$");
3169099ba2SDavid Schultz 
3269099ba2SDavid Schultz #include "namespace.h"
3369099ba2SDavid Schultz #include <sys/param.h>
3469099ba2SDavid Schultz #include <errno.h>
3569099ba2SDavid Schultz #include <limits.h>
3669099ba2SDavid Schultz #include <stdio.h>
3769099ba2SDavid Schultz #include <stdlib.h>
3869099ba2SDavid Schultz #include <string.h>
3969099ba2SDavid Schultz #include "un-namespace.h"
4069099ba2SDavid Schultz 
4169099ba2SDavid Schultz #include "libc_private.h"
4269099ba2SDavid Schultz #include "local.h"
4369099ba2SDavid Schultz 
4469099ba2SDavid Schultz static inline size_t
4569099ba2SDavid Schultz p2roundup(size_t n)
4669099ba2SDavid Schultz {
4769099ba2SDavid Schultz 
4869099ba2SDavid Schultz 	if (!powerof2(n)) {
4969099ba2SDavid Schultz 		n--;
5069099ba2SDavid Schultz 		n |= n >> 1;
5169099ba2SDavid Schultz 		n |= n >> 2;
5269099ba2SDavid Schultz 		n |= n >> 4;
5369099ba2SDavid Schultz 		n |= n >> 8;
5469099ba2SDavid Schultz 		n |= n >> 16;
5569099ba2SDavid Schultz #if SIZE_T_MAX > 0xffffffffU
5669099ba2SDavid Schultz 		n |= n >> 32;
5769099ba2SDavid Schultz #endif
5869099ba2SDavid Schultz 		n++;
5969099ba2SDavid Schultz 	}
6069099ba2SDavid Schultz 	return (n);
6169099ba2SDavid Schultz }
6269099ba2SDavid Schultz 
6369099ba2SDavid Schultz /*
6469099ba2SDavid Schultz  * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
6569099ba2SDavid Schultz  */
6669099ba2SDavid Schultz static inline int
6769099ba2SDavid Schultz expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
6869099ba2SDavid Schultz {
6969099ba2SDavid Schultz 	char *newline;
7069099ba2SDavid Schultz 	size_t newcap;
7169099ba2SDavid Schultz 
7269099ba2SDavid Schultz 	if (len > (size_t)SSIZE_MAX + 1) {
7369099ba2SDavid Schultz 		errno = EOVERFLOW;
7469099ba2SDavid Schultz 		return (-1);
7569099ba2SDavid Schultz 	}
7669099ba2SDavid Schultz 	if (len > *capp) {
7769099ba2SDavid Schultz 		if (len == (size_t)SSIZE_MAX + 1)	/* avoid overflow */
7869099ba2SDavid Schultz 			newcap = (size_t)SSIZE_MAX + 1;
7969099ba2SDavid Schultz 		else
8069099ba2SDavid Schultz 			newcap = p2roundup(len);
8169099ba2SDavid Schultz 		newline = realloc(*linep, newcap);
8269099ba2SDavid Schultz 		if (newline == NULL)
8369099ba2SDavid Schultz 			return (-1);
8469099ba2SDavid Schultz 		*capp = newcap;
8569099ba2SDavid Schultz 		*linep = newline;
8669099ba2SDavid Schultz 	}
8769099ba2SDavid Schultz 	return (0);
8869099ba2SDavid Schultz }
8969099ba2SDavid Schultz 
9069099ba2SDavid Schultz /*
9169099ba2SDavid Schultz  * Append the src buffer to the *dstp buffer. The buffers are of
9269099ba2SDavid Schultz  * length srclen and *dstlenp, respectively, and dst has space for
9369099ba2SDavid Schultz  * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
9469099ba2SDavid Schultz  * appropriately, and *dstp is reallocated if needed. Returns 0 on
9569099ba2SDavid Schultz  * success, -1 on allocation failure.
9669099ba2SDavid Schultz  */
9769099ba2SDavid Schultz static int
9869099ba2SDavid Schultz sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
9969099ba2SDavid Schultz 	size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
10069099ba2SDavid Schultz {
10169099ba2SDavid Schultz 
10269099ba2SDavid Schultz 	/* ensure room for srclen + dstlen + terminating NUL */
10369099ba2SDavid Schultz 	if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
10469099ba2SDavid Schultz 		return (-1);
10569099ba2SDavid Schultz 	memcpy(*dstp + *dstlenp, src, srclen);
10669099ba2SDavid Schultz 	*dstlenp += srclen;
10769099ba2SDavid Schultz 	return (0);
10869099ba2SDavid Schultz }
10969099ba2SDavid Schultz 
11069099ba2SDavid Schultz ssize_t
11169099ba2SDavid Schultz getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
11269099ba2SDavid Schultz 	 FILE * __restrict fp)
11369099ba2SDavid Schultz {
11469099ba2SDavid Schultz 	u_char *endp;
11569099ba2SDavid Schultz 	size_t linelen;
11669099ba2SDavid Schultz 
117fda0a14fSKonstantin Belousov 	FLOCKFILE_CANCELSAFE(fp);
11869099ba2SDavid Schultz 	ORIENT(fp, -1);
11969099ba2SDavid Schultz 
12069099ba2SDavid Schultz 	if (linep == NULL || linecapp == NULL) {
12169099ba2SDavid Schultz 		errno = EINVAL;
12269099ba2SDavid Schultz 		goto error;
12369099ba2SDavid Schultz 	}
12469099ba2SDavid Schultz 
1257e817e2aSDavid Schultz 	if (*linep == NULL)
1267e817e2aSDavid Schultz 		*linecapp = 0;
12769099ba2SDavid Schultz 
12869099ba2SDavid Schultz 	if (fp->_r <= 0 && __srefill(fp)) {
12969099ba2SDavid Schultz 		/* If fp is at EOF already, we just need space for the NUL. */
1301bf6c5f1SAndrey A. Chernov 		if (!__sfeof(fp) || expandtofit(linep, 1, linecapp))
13169099ba2SDavid Schultz 			goto error;
1326685ac34SDavid Schultz 		(*linep)[0] = '\0';
133fda0a14fSKonstantin Belousov 		linelen = -1;
134fda0a14fSKonstantin Belousov 		goto end;
13569099ba2SDavid Schultz 	}
13669099ba2SDavid Schultz 
1376685ac34SDavid Schultz 	linelen = 0;
13869099ba2SDavid Schultz 	while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
13969099ba2SDavid Schultz 		if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r))
14069099ba2SDavid Schultz 			goto error;
14169099ba2SDavid Schultz 		if (__srefill(fp)) {
1421bf6c5f1SAndrey A. Chernov 			if (!__sfeof(fp))
14369099ba2SDavid Schultz 				goto error;
14469099ba2SDavid Schultz 			goto done;	/* hit EOF */
14569099ba2SDavid Schultz 		}
14669099ba2SDavid Schultz 	}
14769099ba2SDavid Schultz 	endp++;	/* snarf the delimiter, too */
14869099ba2SDavid Schultz 	if (sappend(linep, &linelen, linecapp, fp->_p, endp - fp->_p))
14969099ba2SDavid Schultz 		goto error;
15069099ba2SDavid Schultz 	fp->_r -= endp - fp->_p;
15169099ba2SDavid Schultz 	fp->_p = endp;
15269099ba2SDavid Schultz done:
15369099ba2SDavid Schultz 	/* Invariant: *linep has space for at least linelen+1 bytes. */
15469099ba2SDavid Schultz 	(*linep)[linelen] = '\0';
155fda0a14fSKonstantin Belousov end:
156fda0a14fSKonstantin Belousov 	FUNLOCKFILE_CANCELSAFE();
15769099ba2SDavid Schultz 	return (linelen);
15869099ba2SDavid Schultz 
15969099ba2SDavid Schultz error:
16069099ba2SDavid Schultz 	fp->_flags |= __SERR;
161fda0a14fSKonstantin Belousov 	linelen = -1;
162fda0a14fSKonstantin Belousov 	goto end;
16369099ba2SDavid Schultz }
164