| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
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 |
| @@ -18,27 +18,27 @@ | | | @@ -18,27 +18,27 @@ |
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" |
| @@ -80,32 +80,33 @@ __getdelim(char **__restrict buf, size_t | | | @@ -80,32 +80,33 @@ __getdelim(char **__restrict buf, size_t |
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; |