diff options
author | 2016-11-25 23:22:04 +0000 | |
---|---|---|
committer | 2016-11-25 23:22:04 +0000 | |
commit | 66d9cecc96a0968b7e292664dd4ae9918fb04a42 (patch) | |
tree | 96c92e6e7eff5dc896c807a3d5cf7f306f3365cd /usr.bin/ssh/sshbuf.c | |
parent | remove an apostrophe on the advice of the oed: (diff) | |
download | wireguard-openbsd-66d9cecc96a0968b7e292664dd4ae9918fb04a42.tar.xz wireguard-openbsd-66d9cecc96a0968b7e292664dd4ae9918fb04a42.zip |
split allocation out of sshbuf_reserve() into a separate
sshbuf_allocate() function; ok markus@
Diffstat (limited to 'usr.bin/ssh/sshbuf.c')
-rw-r--r-- | usr.bin/ssh/sshbuf.c | 76 |
1 files changed, 43 insertions, 33 deletions
diff --git a/usr.bin/ssh/sshbuf.c b/usr.bin/ssh/sshbuf.c index 9be20028a2d..594ff25500b 100644 --- a/usr.bin/ssh/sshbuf.c +++ b/usr.bin/ssh/sshbuf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sshbuf.c,v 1.7 2016/09/12 01:22:38 deraadt Exp $ */ +/* $OpenBSD: sshbuf.c,v 1.8 2016/11/25 23:22:04 djm Exp $ */ /* * Copyright (c) 2011 Damien Miller * @@ -314,16 +314,13 @@ sshbuf_check_reserve(const struct sshbuf *buf, size_t len) } int -sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) +sshbuf_allocate(struct sshbuf *buf, size_t len) { size_t rlen, need; u_char *dp; int r; - if (dpp != NULL) - *dpp = NULL; - - SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len)); + SSHBUF_DBG(("allocate buf = %p len = %zu", buf, len)); if ((r = sshbuf_check_reserve(buf, len)) != 0) return r; /* @@ -331,36 +328,49 @@ sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) * then pack the buffer, zeroing buf->off. */ sshbuf_maybe_pack(buf, buf->size + len > buf->max_size); - SSHBUF_TELL("reserve"); - if (len + buf->size > buf->alloc) { - /* - * Prefer to alloc in SSHBUF_SIZE_INC units, but - * allocate less if doing so would overflow max_size. - */ - need = len + buf->size - buf->alloc; - rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC); - SSHBUF_DBG(("need %zu initial rlen %zu", need, rlen)); - if (rlen > buf->max_size) - rlen = buf->alloc + need; - SSHBUF_DBG(("adjusted rlen %zu", rlen)); - if ((dp = realloc(buf->d, rlen)) == NULL) { - SSHBUF_DBG(("realloc fail")); - if (dpp != NULL) - *dpp = NULL; - return SSH_ERR_ALLOC_FAIL; - } - buf->alloc = rlen; - buf->cd = buf->d = dp; - if ((r = sshbuf_check_reserve(buf, len)) < 0) { - /* shouldn't fail */ - if (dpp != NULL) - *dpp = NULL; - return r; - } + SSHBUF_TELL("allocate"); + if (len + buf->size <= buf->alloc) + return 0; /* already have it. */ + + /* + * Prefer to alloc in SSHBUF_SIZE_INC units, but + * allocate less if doing so would overflow max_size. + */ + need = len + buf->size - buf->alloc; + rlen = ROUNDUP(buf->alloc + need, SSHBUF_SIZE_INC); + SSHBUF_DBG(("need %zu initial rlen %zu", need, rlen)); + if (rlen > buf->max_size) + rlen = buf->alloc + need; + SSHBUF_DBG(("adjusted rlen %zu", rlen)); + if ((dp = realloc(buf->d, rlen)) == NULL) { + SSHBUF_DBG(("realloc fail")); + return SSH_ERR_ALLOC_FAIL; + } + buf->alloc = rlen; + buf->cd = buf->d = dp; + if ((r = sshbuf_check_reserve(buf, len)) < 0) { + /* shouldn't fail */ + return r; } + SSHBUF_TELL("done"); + return 0; +} + +int +sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp) +{ + u_char *dp; + int r; + + if (dpp != NULL) + *dpp = NULL; + + SSHBUF_DBG(("reserve buf = %p len = %zu", buf, len)); + if ((r = sshbuf_allocate(buf, len)) != 0) + return r; + dp = buf->d + buf->size; buf->size += len; - SSHBUF_TELL("done"); if (dpp != NULL) *dpp = dp; return 0; |