Simplify mem_is_nonzero to mem_is_zero

This also fixes a bug that the value that mem_is_nonzero tried to
return could overflow int.
This commit is contained in:
Gilles Peskine 2018-06-21 09:21:51 +02:00 committed by itayzafrir
parent 818ca1283a
commit 3f669c374a

View file

@ -13,19 +13,19 @@
* \param buffer Pointer to the beginning of the buffer. * \param buffer Pointer to the beginning of the buffer.
* \param size Size of the buffer in bytes. * \param size Size of the buffer in bytes.
* *
* \return 0 if the buffer is all-bits-zero. * \return 1 if the buffer is all-bits-zero.
* \return A nonzero value otherwise. * \return 0 if there is at least one nonzero byte.
*/ */
int mem_is_nonzero( void *buffer, size_t size ) static int mem_is_zero( void *buffer, size_t size )
{ {
size_t i; size_t i;
for( i = 0; i < size; i++ ) for( i = 0; i < size; i++ )
{ {
if( ( (unsigned char *) buffer )[i] != 0 ) if( ( (unsigned char *) buffer )[i] != 0 )
return( i + 1 );
}
return( 0 ); return( 0 );
} }
return( 1 );
}
static int exercise_mac_key( psa_key_slot_t key, static int exercise_mac_key( psa_key_slot_t key,
psa_key_usage_t usage, psa_key_usage_t usage,
@ -349,7 +349,7 @@ void import_export( data_t *data,
exported, export_size, exported, export_size,
&exported_length ); &exported_length );
TEST_ASSERT( status == (psa_status_t) expected_export_status ); TEST_ASSERT( status == (psa_status_t) expected_export_status );
TEST_ASSERT( ! mem_is_nonzero( exported + exported_length, TEST_ASSERT( mem_is_zero( exported + exported_length,
export_size - exported_length ) ); export_size - exported_length ) );
if( status != PSA_SUCCESS ) if( status != PSA_SUCCESS )
{ {