From 3d49b9d220f185318ad4dd419ad286e05944123e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 6 Jun 2014 14:48:09 +0200 Subject: [PATCH] Add test helper function unhexify_alloc() --- tests/suites/helpers.function | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/suites/helpers.function b/tests/suites/helpers.function index f6a35295b..2be5dcce4 100644 --- a/tests/suites/helpers.function +++ b/tests/suites/helpers.function @@ -2,6 +2,13 @@ #include "polarssl/memory.h" #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_malloc malloc +#define polarssl_free free +#endif + #ifdef _MSC_VER #include typedef UINT32 uint32_t; @@ -94,6 +101,27 @@ static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len) } } +/** + * Allocate and fill a buffer from hex data. + * + * The buffer is sized exactly as needed. This allows to detect buffer + * overruns (including overreads) when running the test suite under valgrind. + * + * For convenience, dies if allocation fails. + */ +static unsigned char *unhexify_alloc( const char *ibuf, size_t *olen ) +{ + unsigned char *obuf; + + *olen = strlen(ibuf) / 2; + + assert( ( obuf = polarssl_malloc( *olen ) ) != NULL ); + + (void) unhexify( obuf, ibuf ); + + return( obuf ); +} + /** * This function just returns data from rand(). * Although predictable and often similar on multiple