1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009 David Schultz <das@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "namespace.h" 33 #include <sys/param.h> 34 #include <errno.h> 35 #include <limits.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include "un-namespace.h" 40 41 #include "libc_private.h" 42 #include "local.h" 43 44 static inline size_t 45 p2roundup(size_t n) 46 { 47 48 if (!powerof2(n)) { 49 n--; 50 n |= n >> 1; 51 n |= n >> 2; 52 n |= n >> 4; 53 n |= n >> 8; 54 n |= n >> 16; 55 #if SIZE_T_MAX > 0xffffffffU 56 n |= n >> 32; 57 #endif 58 n++; 59 } 60 return (n); 61 } 62 63 /* 64 * Expand *linep to hold len bytes (up to SSIZE_MAX + 1). 65 */ 66 static inline int 67 expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp) 68 { 69 char *newline; 70 size_t newcap; 71 72 if (len > (size_t)SSIZE_MAX + 1) { 73 errno = EOVERFLOW; 74 return (-1); 75 } 76 if (len > *capp) { 77 if (len == (size_t)SSIZE_MAX + 1) /* avoid overflow */ 78 newcap = (size_t)SSIZE_MAX + 1; 79 else 80 newcap = p2roundup(len); 81 newline = realloc(*linep, newcap); 82 if (newline == NULL) 83 return (-1); 84 *capp = newcap; 85 *linep = newline; 86 } 87 return (0); 88 } 89 90 /* 91 * Append the src buffer to the *dstp buffer. The buffers are of 92 * length srclen and *dstlenp, respectively, and dst has space for 93 * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated 94 * appropriately, and *dstp is reallocated if needed. Returns 0 on 95 * success, -1 on allocation failure. 96 */ 97 static int 98 sappend(char ** __restrict dstp, size_t * __restrict dstlenp, 99 size_t * __restrict dstcapp, char * __restrict src, size_t srclen) 100 { 101 102 /* ensure room for srclen + dstlen + terminating NUL */ 103 if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp)) 104 return (-1); 105 memcpy(*dstp + *dstlenp, src, srclen); 106 *dstlenp += srclen; 107 return (0); 108 } 109 110 ssize_t 111 getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim, 112 FILE * __restrict fp) 113 { 114 u_char *endp; 115 size_t linelen; 116 117 FLOCKFILE_CANCELSAFE(fp); 118 ORIENT(fp, -1); 119 120 if (linep == NULL || linecapp == NULL) { 121 errno = EINVAL; 122 goto error; 123 } 124 125 if (*linep == NULL) 126 *linecapp = 0; 127 128 if (fp->_r <= 0 && __srefill(fp)) { 129 /* If fp is at EOF already, we just need space for the NUL. */ 130 if (!__sfeof(fp) || expandtofit(linep, 1, linecapp)) 131 goto error; 132 (*linep)[0] = '\0'; 133 linelen = -1; 134 goto end; 135 } 136 137 linelen = 0; 138 while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) { 139 if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r)) 140 goto error; 141 if (__srefill(fp)) { 142 if (!__sfeof(fp)) 143 goto error; 144 goto done; /* hit EOF */ 145 } 146 } 147 endp++; /* snarf the delimiter, too */ 148 if (sappend(linep, &linelen, linecapp, fp->_p, endp - fp->_p)) 149 goto error; 150 fp->_r -= endp - fp->_p; 151 fp->_p = endp; 152 done: 153 /* Invariant: *linep has space for at least linelen+1 bytes. */ 154 (*linep)[linelen] = '\0'; 155 end: 156 FUNLOCKFILE_CANCELSAFE(); 157 return (linelen); 158 159 error: 160 fp->_flags |= __SERR; 161 linelen = -1; 162 goto end; 163 } 164