Change ASSERT_ALLOC to take a size in elements, not bytes

`ASSERT_ALLOC(p, length)` now allocates `length` elements, i.e.
`length * sizeof(*p)` bytes.
This commit is contained in:
Gilles Peskine 2018-11-30 18:51:45 +01:00
parent 69f976b1d6
commit 7f6e3a868a

View file

@ -95,7 +95,7 @@ typedef struct data_tag
* You must set \p pointer to \c NULL before calling this macro and * You must set \p pointer to \c NULL before calling this macro and
* put `mbedtls_free( pointer )` in the test's cleanup code. * put `mbedtls_free( pointer )` in the test's cleanup code.
* *
* If \p size is zero, the resulting \p pointer will be \c NULL. * If \p length is zero, the resulting \p pointer will be \c NULL.
* This is usually what we want in tests since API functions are * This is usually what we want in tests since API functions are
* supposed to accept null pointers when a buffer size is zero. * supposed to accept null pointers when a buffer size is zero.
* *
@ -105,17 +105,18 @@ typedef struct data_tag
* \param pointer An lvalue where the address of the allocated buffer * \param pointer An lvalue where the address of the allocated buffer
* will be stored. * will be stored.
* This expression may be evaluated multiple times. * This expression may be evaluated multiple times.
* \param size Buffer size to allocate in bytes. * \param length Number of elements to allocate.
* This expression may be evaluated multiple times. * This expression may be evaluated multiple times.
* *
*/ */
#define ASSERT_ALLOC( pointer, size ) \ #define ASSERT_ALLOC( pointer, length ) \
do \ do \
{ \ { \
TEST_ASSERT( ( pointer ) == NULL ); \ TEST_ASSERT( ( pointer ) == NULL ); \
if( ( size ) != 0 ) \ if( ( length ) != 0 ) \
{ \ { \
( pointer ) = mbedtls_calloc( 1, ( size ) ); \ ( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
( length ) ); \
TEST_ASSERT( ( pointer ) != NULL ); \ TEST_ASSERT( ( pointer ) != NULL ); \
} \ } \
} \ } \