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 <janos.follath@arm.com>
This commit is contained in:
Janos Follath 2022-11-26 14:28:50 +00:00
parent 6ef582f2b8
commit 75b9f0fd2e

View File

@ -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)