From 7f2c4a2bceadf5ab1cfcfc982ac2f57552b4a1e9 Mon Sep 17 00:00:00 2001 From: jsing Date: Sun, 9 Apr 2017 15:06:20 +0000 Subject: [PATCH] Simplify/clean up BUF_MEM_grow_clean(). ok beck@ --- lib/libcrypto/buffer/buffer.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/libcrypto/buffer/buffer.c b/lib/libcrypto/buffer/buffer.c index f15b93d26c0..5ed893f5f95 100644 --- a/lib/libcrypto/buffer/buffer.c +++ b/lib/libcrypto/buffer/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.25 2017/04/09 15:03:54 jsing Exp $ */ +/* $OpenBSD: buffer.c,v 1.26 2017/04/09 15:06:20 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -63,9 +63,11 @@ #include #include -/* LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That - * function is applied in several functions in this file and this limit ensures - * that the result fits in an int. */ +/* + * LIMIT_BEFORE_EXPANSION is the maximum n such that (n + 3) / 3 * 4 < 2**31. + * That function is applied in several functions in this file and this limit + * ensures that the result fits in an int. + */ #define LIMIT_BEFORE_EXPANSION 0x5ffffffc BUF_MEM * @@ -106,30 +108,27 @@ BUF_MEM_grow_clean(BUF_MEM *str, size_t len) char *ret; size_t n; - if (str->length >= len) { - memset(&str->data[len], 0, str->length - len); - str->length = len; - return (len); - } if (str->max >= len) { + if (str->length >= len) + memset(&str->data[len], 0, str->length - len); str->length = len; return (len); } - /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */ + if (len > LIMIT_BEFORE_EXPANSION) { BUFerror(ERR_R_MALLOC_FAILURE); return 0; } + n = (len + 3) / 3 * 4; - ret = recallocarray(str->data, str->max, n, 1); - if (ret == NULL) { + if ((ret = recallocarray(str->data, str->max, n, 1)) == NULL) { BUFerror(ERR_R_MALLOC_FAILURE); - len = 0; - } else { - str->data = ret; - str->max = n; - str->length = len; + return (0); } + str->data = ret; + str->max = n; + str->length = len; + return (len); } -- 2.20.1