diff options
author | jsing <> | 2017-03-16 13:29:56 +0000 |
---|---|---|
committer | jsing <> | 2017-03-16 13:29:56 +0000 |
commit | 854a4a0e4ddf686eb6d9e36d8b0af8926c723a28 (patch) | |
tree | 91836b909bf0ba30f481ebe88e5bc4cf8620c2ea | |
parent | 9876415607a0e48169e9eaf668be954816a65715 (diff) | |
download | openbsd-854a4a0e4ddf686eb6d9e36d8b0af8926c723a28.tar.gz openbsd-854a4a0e4ddf686eb6d9e36d8b0af8926c723a28.tar.bz2 openbsd-854a4a0e4ddf686eb6d9e36d8b0af8926c723a28.zip |
Convert BUF_MEM_grow() and BUF_MEM_grow_clean() to recallocarray(),
ensuring that the buffer contents are zeroed on allocation and not leaked
when resizing.
It is worth noting that BUF_MEM_grow_clean() already did this manually by
avoiding realloc().
ok beck@ inoguchi@
-rw-r--r-- | src/lib/libcrypto/buffer/buffer.c | 16 |
1 files changed, 3 insertions, 13 deletions
diff --git a/src/lib/libcrypto/buffer/buffer.c b/src/lib/libcrypto/buffer/buffer.c index ddc8f39408..2e4959a58d 100644 --- a/src/lib/libcrypto/buffer/buffer.c +++ b/src/lib/libcrypto/buffer/buffer.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: buffer.c,v 1.23 2017/03/16 13:15:06 jsing Exp $ */ | 1 | /* $OpenBSD: buffer.c,v 1.24 2017/03/16 13:29:56 jsing Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -105,7 +105,6 @@ BUF_MEM_grow(BUF_MEM *str, size_t len) | |||
105 | return (len); | 105 | return (len); |
106 | } | 106 | } |
107 | if (str->max >= len) { | 107 | if (str->max >= len) { |
108 | memset(&str->data[str->length], 0, len - str->length); | ||
109 | str->length = len; | 108 | str->length = len; |
110 | return (len); | 109 | return (len); |
111 | } | 110 | } |
@@ -115,14 +114,13 @@ BUF_MEM_grow(BUF_MEM *str, size_t len) | |||
115 | return 0; | 114 | return 0; |
116 | } | 115 | } |
117 | n = (len + 3) / 3 * 4; | 116 | n = (len + 3) / 3 * 4; |
118 | ret = realloc(str->data, n); | 117 | ret = recallocarray(str->data, str->max, n, 1); |
119 | if (ret == NULL) { | 118 | if (ret == NULL) { |
120 | BUFerror(ERR_R_MALLOC_FAILURE); | 119 | BUFerror(ERR_R_MALLOC_FAILURE); |
121 | len = 0; | 120 | len = 0; |
122 | } else { | 121 | } else { |
123 | str->data = ret; | 122 | str->data = ret; |
124 | str->max = n; | 123 | str->max = n; |
125 | memset(&str->data[str->length], 0, len - str->length); | ||
126 | str->length = len; | 124 | str->length = len; |
127 | } | 125 | } |
128 | return (len); | 126 | return (len); |
@@ -140,7 +138,6 @@ BUF_MEM_grow_clean(BUF_MEM *str, size_t len) | |||
140 | return (len); | 138 | return (len); |
141 | } | 139 | } |
142 | if (str->max >= len) { | 140 | if (str->max >= len) { |
143 | memset(&str->data[str->length], 0, len - str->length); | ||
144 | str->length = len; | 141 | str->length = len; |
145 | return (len); | 142 | return (len); |
146 | } | 143 | } |
@@ -150,20 +147,13 @@ BUF_MEM_grow_clean(BUF_MEM *str, size_t len) | |||
150 | return 0; | 147 | return 0; |
151 | } | 148 | } |
152 | n = (len + 3) / 3 * 4; | 149 | n = (len + 3) / 3 * 4; |
153 | ret = malloc(n); | 150 | ret = recallocarray(str->data, str->max, n, 1); |
154 | /* we're not shrinking - that case returns above */ | ||
155 | if ((ret != NULL) && (str->data != NULL)) { | ||
156 | memcpy(ret, str->data, str->max); | ||
157 | explicit_bzero(str->data, str->max); | ||
158 | free(str->data); | ||
159 | } | ||
160 | if (ret == NULL) { | 151 | if (ret == NULL) { |
161 | BUFerror(ERR_R_MALLOC_FAILURE); | 152 | BUFerror(ERR_R_MALLOC_FAILURE); |
162 | len = 0; | 153 | len = 0; |
163 | } else { | 154 | } else { |
164 | str->data = ret; | 155 | str->data = ret; |
165 | str->max = n; | 156 | str->max = n; |
166 | memset(&str->data[str->length], 0, len - str->length); | ||
167 | str->length = len; | 157 | str->length = len; |
168 | } | 158 | } |
169 | return (len); | 159 | return (len); |