summaryrefslogtreecommitdiff
path: root/sshbuf.c
diff options
context:
space:
mode:
authorderaadt@openbsd.org <deraadt@openbsd.org>2017-05-31 09:15:42 +0000
committerDamien Miller <djm@mindrot.org>2017-06-01 14:55:22 +1000
commit9e509d4ec97cb3d71696f1a2f1fdad254cbbce11 (patch)
tree8f33ae8fa9bcfa0d9c80d0e0f1555a814a844bc1 /sshbuf.c
parentdc5dc45662773c0f7745c29cf77ae2d52723e55e (diff)
upstream commit
Switch to recallocarray() for a few operations. Both growth and shrinkage are handled safely, and there also is no need for preallocation dances. Future changes in this area will be less error prone. Review and one bug found by markus Upstream-ID: 822d664d6a5a1d10eccb23acdd53578a679d5065
Diffstat (limited to 'sshbuf.c')
-rw-r--r--sshbuf.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/sshbuf.c b/sshbuf.c
index 652c99a21..b7a90b5c2 100644
--- a/sshbuf.c
+++ b/sshbuf.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshbuf.c,v 1.9 2017/05/26 20:34:49 markus Exp $ */ 1/* $OpenBSD: sshbuf.c,v 1.10 2017/05/31 09:15:42 deraadt Exp $ */
2/* 2/*
3 * Copyright (c) 2011 Damien Miller 3 * Copyright (c) 2011 Damien Miller
4 * 4 *
@@ -193,15 +193,16 @@ sshbuf_reset(struct sshbuf *buf)
193 buf->off = buf->size; 193 buf->off = buf->size;
194 return; 194 return;
195 } 195 }
196 if (sshbuf_check_sanity(buf) == 0) 196 (void) sshbuf_check_sanity(buf);
197 explicit_bzero(buf->d, buf->alloc);
198 buf->off = buf->size = 0; 197 buf->off = buf->size = 0;
199 if (buf->alloc != SSHBUF_SIZE_INIT) { 198 if (buf->alloc != SSHBUF_SIZE_INIT) {
200 if ((d = realloc(buf->d, SSHBUF_SIZE_INIT)) != NULL) { 199 if ((d = recallocarray(buf->d, buf->alloc, SSHBUF_SIZE_INIT,
200 1)) != NULL) {
201 buf->cd = buf->d = d; 201 buf->cd = buf->d = d;
202 buf->alloc = SSHBUF_SIZE_INIT; 202 buf->alloc = SSHBUF_SIZE_INIT;
203 } 203 }
204 } 204 } else
205 explicit_bzero(buf->d, SSHBUF_SIZE_INIT);
205} 206}
206 207
207size_t 208size_t
@@ -253,9 +254,8 @@ sshbuf_set_max_size(struct sshbuf *buf, size_t max_size)
253 rlen = ROUNDUP(buf->size, SSHBUF_SIZE_INC); 254 rlen = ROUNDUP(buf->size, SSHBUF_SIZE_INC);
254 if (rlen > max_size) 255 if (rlen > max_size)
255 rlen = max_size; 256 rlen = max_size;
256 explicit_bzero(buf->d + buf->size, buf->alloc - buf->size);
257 SSHBUF_DBG(("new alloc = %zu", rlen)); 257 SSHBUF_DBG(("new alloc = %zu", rlen));
258 if ((dp = realloc(buf->d, rlen)) == NULL) 258 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL)
259 return SSH_ERR_ALLOC_FAIL; 259 return SSH_ERR_ALLOC_FAIL;
260 buf->cd = buf->d = dp; 260 buf->cd = buf->d = dp;
261 buf->alloc = rlen; 261 buf->alloc = rlen;
@@ -344,7 +344,7 @@ sshbuf_allocate(struct sshbuf *buf, size_t len)
344 if (rlen > buf->max_size) 344 if (rlen > buf->max_size)
345 rlen = buf->alloc + need; 345 rlen = buf->alloc + need;
346 SSHBUF_DBG(("adjusted rlen %zu", rlen)); 346 SSHBUF_DBG(("adjusted rlen %zu", rlen));
347 if ((dp = realloc(buf->d, rlen)) == NULL) { 347 if ((dp = recallocarray(buf->d, buf->alloc, rlen, 1)) == NULL) {
348 SSHBUF_DBG(("realloc fail")); 348 SSHBUF_DBG(("realloc fail"));
349 return SSH_ERR_ALLOC_FAIL; 349 return SSH_ERR_ALLOC_FAIL;
350 } 350 }