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