Added API unit tests for new BUF_MEM. Fixed wolfSSL_BUF_MEM_grow handling of negative “len” input. Added GPLv2 header to new buffer.h.

This commit is contained in:
David Garske 2017-01-18 11:04:32 -08:00 committed by Jacob Barthelmeh
parent 2a4ea5843d
commit aac050369a
3 changed files with 48 additions and 13 deletions

View File

@ -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)

View File

@ -216,6 +216,7 @@
#include <wolfssl/openssl/evp.h>
#include <wolfssl/openssl/dh.h>
#include <wolfssl/openssl/bn.h>
#include <wolfssl/openssl/buffer.h>
#include <wolfssl/openssl/pem.h>
#include <wolfssl/openssl/ec.h>
#include <wolfssl/openssl/engine.h>
@ -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();

View File

@ -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_