From cc93908b88076cf02b1bbd02a11fb2fc4b35ccb1 Mon Sep 17 00:00:00 2001 From: Janos Follath Date: Mon, 15 Aug 2022 12:08:49 +0100 Subject: [PATCH] Bignum: Declare loop variable in loop head In the new bignum files (bignum_core.c, bignum_mod_raw.c and bignum_mod.c) the loop variables are declared in the loop head wherever this change is beneficial. There are loops where the loop variable is used after the end of the loop (this might not be good practice, but that is out of scope for this commit) and others where there are several loop variables and declaring them there would hurt readability. Signed-off-by: Janos Follath --- library/bignum_core.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/library/bignum_core.c b/library/bignum_core.c index 431921ade..c015fc7ac 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -216,7 +216,6 @@ int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X, { size_t stored_bytes = nx * ciL; size_t bytes_to_copy; - size_t i; if( stored_bytes < buflen ) { @@ -228,14 +227,14 @@ int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X, /* The output buffer is smaller than the allocated size of X. * However X may fit if its leading bytes are zero. */ - for( i = bytes_to_copy; i < stored_bytes; i++ ) + for( size_t i = bytes_to_copy; i < stored_bytes; i++ ) { if( GET_BYTE( X, i ) != 0 ) return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); } } - for( i = 0; i < bytes_to_copy; i++ ) + for( size_t i = 0; i < bytes_to_copy; i++ ) buf[i] = GET_BYTE( X, i ); if( stored_bytes < buflen ) @@ -255,7 +254,6 @@ int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, size_t stored_bytes; size_t bytes_to_copy; unsigned char *p; - size_t i; stored_bytes = nx * ciL; @@ -276,14 +274,14 @@ int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, * However X may fit if its leading bytes are zero. */ bytes_to_copy = buflen; p = buf; - for( i = bytes_to_copy; i < stored_bytes; i++ ) + for( size_t i = bytes_to_copy; i < stored_bytes; i++ ) { if( GET_BYTE( X, i ) != 0 ) return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); } } - for( i = 0; i < bytes_to_copy; i++ ) + for( size_t i = 0; i < bytes_to_copy; i++ ) p[bytes_to_copy - i - 1] = GET_BYTE( X, i ); return( 0 );