xref: /openbsd-src/lib/libc/stdio/getdelim.c (revision 5b859c19fe53bbea08f5c342e0a4470e99f883e1)
1 /*	$OpenBSD: getdelim.c,v 1.2 2014/10/16 17:31:51 millert Exp $	*/
2 /* $NetBSD: getdelim.c,v 1.13 2011/07/22 23:12:30 joerg Exp $ */
3 
4 /*
5  * Copyright (c) 2009 The NetBSD Foundation, Inc.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Roy Marples.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "local.h"
38 
39 /* Minimum buffer size we create.
40  * This should allow config files to fit into our power of 2 buffer growth
41  * without the need for a realloc. */
42 #define MINBUF	128
43 
44 ssize_t
45 getdelim(char **__restrict buf, size_t *__restrict buflen,
46     int sep, FILE *__restrict fp)
47 {
48 	unsigned char *p;
49 	size_t len, newlen, off;
50 	char *newb;
51 
52 	FLOCKFILE(fp);
53 
54 	if (buf == NULL || buflen == NULL) {
55 		errno = EINVAL;
56 		goto error;
57 	}
58 
59 	/* If buf is NULL, we have to assume a size of zero */
60 	if (*buf == NULL)
61 		*buflen = 0;
62 
63 	_SET_ORIENTATION(fp, -1);
64 	off = 0;
65 	do {
66 		/* If the input buffer is empty, refill it */
67 		if (fp->_r <= 0 && __srefill(fp)) {
68 			if (__sferror(fp))
69 				goto error;
70 			/* No error, so EOF. */
71 			break;
72 		}
73 
74 		/* Scan through looking for the separator */
75 		p = memchr(fp->_p, sep, (size_t)fp->_r);
76 		if (p == NULL)
77 			len = fp->_r;
78 		else
79 			len = (p - fp->_p) + 1;
80 
81 		/* Ensure we can handle it */
82 		if (off > SSIZE_MAX || len + 1 > SSIZE_MAX - off) {
83 			errno = EOVERFLOW;
84 			goto error;
85 		}
86 		newlen = off + len + 1; /* reserve space for NUL terminator */
87 		if (newlen > *buflen) {
88 			if (newlen < MINBUF)
89 				newlen = MINBUF;
90 #define powerof2(x) ((((x)-1)&(x))==0)
91 			if (!powerof2(newlen)) {
92 				/* Grow the buffer to the next power of 2 */
93 				newlen--;
94 				newlen |= newlen >> 1;
95 				newlen |= newlen >> 2;
96 				newlen |= newlen >> 4;
97 				newlen |= newlen >> 8;
98 				newlen |= newlen >> 16;
99 #if SIZE_T_MAX > 0xffffffffU
100 				newlen |= newlen >> 32;
101 #endif
102 				newlen++;
103 			}
104 
105 			newb = realloc(*buf, newlen);
106 			if (newb == NULL)
107 				goto error;
108 			*buf = newb;
109 			*buflen = newlen;
110 		}
111 
112 		(void)memcpy((*buf + off), fp->_p, len);
113 		/* Safe, len is never greater than what fp->_r can fit. */
114 		fp->_r -= (int)len;
115 		fp->_p += (int)len;
116 		off += len;
117 	} while (p == NULL);
118 
119 	FUNLOCKFILE(fp);
120 
121 	/* POSIX demands we return -1 on EOF. */
122 	if (off == 0)
123 		return -1;
124 
125 	if (*buf != NULL)
126 		*(*buf + off) = '\0';
127 	return off;
128 
129 error:
130 	fp->_flags |= __SERR;
131 	FUNLOCKFILE(fp);
132 	return -1;
133 }
134