From aac050369ab4c63916f1ff5e63a23b6fdea03473 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 18 Jan 2017 11:04:32 -0800 Subject: [PATCH] =?UTF-8?q?Added=20API=20unit=20tests=20for=20new=20BUF=5F?= =?UTF-8?q?MEM.=20Fixed=20wolfSSL=5FBUF=5FMEM=5Fgrow=20handling=20of=20neg?= =?UTF-8?q?ative=20=E2=80=9Clen=E2=80=9D=20input.=20Added=20GPLv2=20header?= =?UTF-8?q?=20to=20new=20buffer.h.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ssl.c | 25 +++++++++++++------------ tests/api.c | 14 ++++++++++++++ wolfssl/openssl/buffer.h | 22 +++++++++++++++++++++- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index adea86c1a..6d0ed138f 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -25140,42 +25140,43 @@ WOLFSSL_BUF_MEM* wolfSSL_BUF_MEM_new(void) int wolfSSL_BUF_MEM_grow(WOLFSSL_BUF_MEM* buf, size_t len) { - size_t n; + int len_int = (int)len; + int max; /* verify provided arguments */ - if (buf == NULL) { - return BAD_FUNC_ARG; + if (buf == NULL || len_int < 0) { + return 0; /* BAD_FUNC_ARG; */ } - /* check to see if buffer is already big enough */ + /* check to see if fits in existing length */ if (buf->length > len) { buf->length = len; - return (int)len; + return len_int; } - /* check to see if buffer max fits */ + /* check to see if fits in max buffer */ if (buf->max >= len) { if (buf->data != NULL) { XMEMSET(&buf->data[buf->length], 0, len - buf->length); } buf->length = len; - return (int)len; + return len_int; } /* expand size, to handle growth */ - n = (len + 3) / 3 * 4; + max = (len_int + 3) / 3 * 4; /* use realloc */ - buf->data = (char*)XREALLOC(buf->data, n, NULL, DYNAMIC_TYPE_TMP_BUFFER); + buf->data = (char*)XREALLOC(buf->data, max, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (buf->data == NULL) { - return ERR_R_MALLOC_FAILURE; + return 0; /* ERR_R_MALLOC_FAILURE; */ } - buf->max = n; + buf->max = max; XMEMSET(&buf->data[buf->length], 0, len - buf->length); buf->length = len; - return (int)len; + return len_int; } void wolfSSL_BUF_MEM_free(WOLFSSL_BUF_MEM* buf) diff --git a/tests/api.c b/tests/api.c index 606377cf9..733e942b2 100644 --- a/tests/api.c +++ b/tests/api.c @@ -216,6 +216,7 @@ #include #include #include + #include #include #include #include @@ -14689,6 +14690,18 @@ static void test_wolfSSL_RAND(void) } +static void test_wolfSSL_BUF(void) +{ + #if defined(OPENSSL_EXTRA) + BUF_MEM* buf; + AssertNotNull(buf = BUF_MEM_new()); + AssertIntEQ(BUF_MEM_grow(buf, 10), 10); + AssertIntEQ(BUF_MEM_grow(buf, -1), 0); + BUF_MEM_free(buf); + #endif /* OPENSSL_EXTRA */ +} + + static void test_no_op_functions(void) { #if defined(OPENSSL_EXTRA) @@ -15478,6 +15491,7 @@ void ApiTest(void) test_wolfSSL_ASN1_STRING(); test_wolfSSL_X509(); test_wolfSSL_RAND(); + test_wolfSSL_BUF(); test_wolfSSL_DES_ecb_encrypt(); test_wolfSSL_set_tlsext_status_type(); test_wolfSSL_ASN1_TIME_adj(); diff --git a/wolfssl/openssl/buffer.h b/wolfssl/openssl/buffer.h index 353b04550..9cf5bc077 100644 --- a/wolfssl/openssl/buffer.h +++ b/wolfssl/openssl/buffer.h @@ -1,4 +1,24 @@ -/* buffer.h for openssl */ +/* buffer.h + * + * Copyright (C) 2006-2016 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + #ifndef WOLFSSL_BUFFER_H_ #define WOLFSSL_BUFFER_H_