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 "namespace.h"
31 #include <sys/param.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "un-namespace.h"
38
39 #include "libc_private.h"
40 #include "local.h"
41
42 static inline size_t
p2roundup(size_t n)43 p2roundup(size_t n)
44 {
45
46 if (!powerof2(n)) {
47 n--;
48 n |= n >> 1;
49 n |= n >> 2;
50 n |= n >> 4;
51 n |= n >> 8;
52 n |= n >> 16;
53 #if SIZE_T_MAX > 0xffffffffU
54 n |= n >> 32;
55 #endif
56 n++;
57 }
58 return (n);
59 }
60
61 /*
62 * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
63 */
64 static inline int
expandtofit(char ** __restrict linep,size_t len,size_t * __restrict capp)65 expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
66 {
67 char *newline;
68 size_t newcap;
69
70 if (len > (size_t)SSIZE_MAX + 1) {
71 errno = EOVERFLOW;
72 return (-1);
73 }
74 if (len > *capp) {
75 if (len == (size_t)SSIZE_MAX + 1) /* avoid overflow */
76 newcap = (size_t)SSIZE_MAX + 1;
77 else
78 newcap = p2roundup(len);
79 newline = realloc(*linep, newcap);
80 if (newline == NULL)
81 return (-1);
82 *capp = newcap;
83 *linep = newline;
84 }
85 return (0);
86 }
87
88 /*
89 * Append the src buffer to the *dstp buffer. The buffers are of
90 * length srclen and *dstlenp, respectively, and dst has space for
91 * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
92 * appropriately, and *dstp is reallocated if needed. Returns 0 on
93 * success, -1 on allocation failure.
94 */
95 static int
sappend(char ** __restrict dstp,size_t * __restrict dstlenp,size_t * __restrict dstcapp,char * __restrict src,size_t srclen)96 sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
97 size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
98 {
99
100 /* ensure room for srclen + dstlen + terminating NUL */
101 if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
102 return (-1);
103 memcpy(*dstp + *dstlenp, src, srclen);
104 *dstlenp += srclen;
105 return (0);
106 }
107
108 ssize_t
getdelim(char ** __restrict linep,size_t * __restrict linecapp,int delim,FILE * __restrict fp)109 getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
110 FILE * __restrict fp)
111 {
112 u_char *endp;
113 size_t linelen;
114
115 FLOCKFILE_CANCELSAFE(fp);
116 ORIENT(fp, -1);
117
118 if (linep == NULL || linecapp == NULL) {
119 errno = EINVAL;
120 goto error;
121 }
122
123 if (*linep == NULL)
124 *linecapp = 0;
125
126 if (fp->_r <= 0 && __srefill(fp)) {
127 /* If fp is at EOF already, we just need space for the NUL. */
128 if (!__sfeof(fp) || expandtofit(linep, 1, linecapp))
129 goto error;
130 (*linep)[0] = '\0';
131 linelen = -1;
132 goto end;
133 }
134
135 linelen = 0;
136 while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
137 if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r))
138 goto error;
139 errno = 0;
140 if (__srefill(fp)) {
141 if (__sfeof(fp))
142 goto done;
143 if (errno == EAGAIN) {
144 /*
145 * We need to undo a partial read that has
146 * been placed into linep or we would otherwise
147 * lose it on the next read.
148 */
149 while (linelen > 0) {
150 if (__ungetc((*linep)[--linelen],
151 fp) == EOF)
152 goto error;
153 }
154 /*
155 * This is not strictly needed but it is
156 * possible a consumer has worked around an
157 * older EAGAIN bug by buffering a partial
158 * return.
159 */
160 (*linep)[0] = '\0';
161 }
162 goto error;
163 }
164 }
165 endp++; /* snarf the delimiter, too */
166 if (sappend(linep, &linelen, linecapp, fp->_p, endp - fp->_p))
167 goto error;
168 fp->_r -= endp - fp->_p;
169 fp->_p = endp;
170 done:
171 /* Invariant: *linep has space for at least linelen+1 bytes. */
172 (*linep)[linelen] = '\0';
173 end:
174 FUNLOCKFILE_CANCELSAFE();
175 return (linelen);
176
177 error:
178 fp->_flags |= __SERR;
179 linelen = -1;
180 goto end;
181 }
182