From 75b9f0fd2e463ce748dbd44efb1fd1fecbd26d89 Mon Sep 17 00:00:00 2001 From: Janos Follath Date: Sat, 26 Nov 2022 14:28:50 +0000 Subject: [PATCH] mbedtls_mpi_mod_read/write: remove redundant checks The function isn't documented as accepting null pointer, and there's no reason why it should be. Just let it dereference the pointer. The null/zero checks are only marginally useful: they validate that m and r are properly populated objects, not freshly initialized ones. For that, it's enough to check that the pointers aren't null or that the sizes aren't zero, we don't need to check both. Also, use separate if statements for unrelated checks. Signed-off-by: Janos Follath --- library/bignum_mod.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/library/bignum_mod.c b/library/bignum_mod.c index 3cb3c436d9..f07307ce5a 100644 --- a/library/bignum_mod.c +++ b/library/bignum_mod.c @@ -203,11 +203,11 @@ int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r, { int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; - if ( r == NULL || m == NULL ) - goto cleanup; - if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs || - r->limbs == 0 || m->limbs == 0 ) + /* Do our best to check if r and m have been set up */ + if ( r->limbs == 0 || m->limbs == 0 ) + goto cleanup; + if ( r->limbs > m->limbs ) goto cleanup; ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep ); @@ -232,11 +232,10 @@ int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r, { int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; - if ( r == NULL || m == NULL ) + /* Do our best to check if r and m have been set up */ + if ( r->limbs == 0 || m->limbs == 0 ) goto cleanup; - - if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs || - r->limbs == 0 || m->limbs == 0 ) + if ( r->limbs > m->limbs ) goto cleanup; if ( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)