aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGilles Peskine <Gilles.Peskine@arm.com>2022-12-08 19:50:29 +0100
committerGilles Peskine <Gilles.Peskine@arm.com>2022-12-16 10:13:29 +0100
commitd008abbc4f4df748a64aa436e499c7a9270f0522 (patch)
treec9d7827ed6c1e3535553ea31d6ffdb6b1a0e560e
parenta57cf9813a932c3a0a6a9c526b94560d36da39bd (diff)
downloadmbedtls-d008abbc4f4df748a64aa436e499c7a9270f0522.zip
mbedtls-d008abbc4f4df748a64aa436e499c7a9270f0522.tar.gz
mbedtls-d008abbc4f4df748a64aa436e499c7a9270f0522.tar.bz2
Fix leak of modulus structures in tests
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
-rw-r--r--tests/include/test/bignum_helpers.h10
-rw-r--r--tests/src/bignum_helpers.c6
-rw-r--r--tests/suites/test_suite_bignum_random.function2
3 files changed, 16 insertions, 2 deletions
diff --git a/tests/include/test/bignum_helpers.h b/tests/include/test/bignum_helpers.h
index ab3c86a..164017e 100644
--- a/tests/include/test/bignum_helpers.h
+++ b/tests/include/test/bignum_helpers.h
@@ -61,7 +61,8 @@ int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs,
* the length of the input. In other words, it preserves leading zeros.
*
* The limb array is allocated with mbedtls_calloc() and must later be
- * freed with mbedtls_free().
+ * freed with mbedtls_free(). You can do that by calling
+ * mbedtls_test_mpi_mod_modulus_free_with_limbs().
*
* \param[in,out] N A modulus structure. It must be initialized, but
* not set up.
@@ -74,6 +75,13 @@ int mbedtls_test_read_mpi_modulus( mbedtls_mpi_mod_modulus *N,
const char *s,
mbedtls_mpi_mod_rep_selector int_rep );
+/** Free a modulus and its limbs.
+ *
+ * \param[in] N A modulus structure such that there is no other
+ * reference to `N->p`.
+ */
+void mbedtls_test_mpi_mod_modulus_free_with_limbs( mbedtls_mpi_mod_modulus *N );
+
/** Read an MPI from a hexadecimal string.
*
* Like mbedtls_mpi_read_string(), but with tighter guarantees around
diff --git a/tests/src/bignum_helpers.c b/tests/src/bignum_helpers.c
index eb819f5..d6ec9bd 100644
--- a/tests/src/bignum_helpers.c
+++ b/tests/src/bignum_helpers.c
@@ -102,6 +102,12 @@ int mbedtls_test_read_mpi_modulus( mbedtls_mpi_mod_modulus *N,
return( ret );
}
+void mbedtls_test_mpi_mod_modulus_free_with_limbs( mbedtls_mpi_mod_modulus *N )
+{
+ mbedtls_free( (mbedtls_mpi_uint*) N->p );
+ mbedtls_mpi_mod_modulus_free( N );
+}
+
int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )
{
int negative = 0;
diff --git a/tests/suites/test_suite_bignum_random.function b/tests/suites/test_suite_bignum_random.function
index a837e1b..5064a62 100644
--- a/tests/suites/test_suite_bignum_random.function
+++ b/tests/suites/test_suite_bignum_random.function
@@ -208,7 +208,7 @@ void mpi_mod_random_values( int min, char *max_hex )
&rnd_mod_raw, sizeof( rnd_mod_raw ) );
exit:
- mbedtls_mpi_mod_modulus_free( &N );
+ mbedtls_test_mpi_mod_modulus_free_with_limbs( &N );
mbedtls_free( R_core );
mbedtls_free( R_mod_raw );
}