From 79341a4e7ec46858096cc601fd51dc617e9e24f3 Mon Sep 17 00:00:00 2001 From: Werner Lewis Date: Tue, 13 Dec 2022 17:19:01 +0000 Subject: [PATCH] Reallocate X_raw to enforce no overflow Signed-off-by: Werner Lewis --- tests/suites/test_suite_bignum_mod.function | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/suites/test_suite_bignum_mod.function b/tests/suites/test_suite_bignum_mod.function index d65bb6e2d..abe4dee40 100644 --- a/tests/suites/test_suite_bignum_mod.function +++ b/tests/suites/test_suite_bignum_mod.function @@ -237,30 +237,40 @@ void mpi_mod_add( char * input_N, size_t limbs = m.limbs; size_t bytes = limbs * sizeof( *X_raw ); - /* One spare limb for negative testing */ - ASSERT_ALLOC( X_raw, limbs + 1 ); - if( expected_ret == 0 ) { /* Negative test with too many limbs in output */ + ASSERT_ALLOC( X_raw, limbs + 1 ); + x.p = X_raw; x.limbs = limbs + 1; TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_add( &x, &a, &b, &m ) ); + mbedtls_free( X_raw ); + X_raw = NULL; + /* Negative test with too few limbs in output */ if( limbs > 1 ) { + ASSERT_ALLOC( X_raw, limbs - 1 ); + x.p = X_raw; x.limbs = limbs - 1; TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA, mbedtls_mpi_mod_add( &x, &a, &b, &m ) ); + + mbedtls_free( X_raw ); + X_raw = NULL; } /* Negative testing with too many/too few limbs in a and b is covered by * manually-written test cases with oret != 0. */ } + /* Allocate correct number of limbs for X_raw */ + ASSERT_ALLOC( X_raw, limbs ); + TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &m, X_raw, limbs ) ); /* A + B => Correct result or expected error */