Skip to content

Commit 8b38978

Browse files
authored
Merge pull request #349 from gilles-peskine-arm/coverity-20200115-crypto
Fix minor defects found by Coverity
2 parents 358462d + 84984ae commit 8b38978

File tree

6 files changed

+20
-21
lines changed

6 files changed

+20
-21
lines changed

library/cipher.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
527527

528528
*olen = 0;
529529
block_size = mbedtls_cipher_get_block_size( ctx );
530+
if ( 0 == block_size )
531+
{
532+
return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
533+
}
530534

531535
if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
532536
{
@@ -562,11 +566,6 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
562566
}
563567
#endif
564568

565-
if ( 0 == block_size )
566-
{
567-
return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
568-
}
569-
570569
if( input == output &&
571570
( ctx->unprocessed_len != 0 || ilen % block_size ) )
572571
{
@@ -625,11 +624,6 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
625624
*/
626625
if( 0 != ilen )
627626
{
628-
if( 0 == block_size )
629-
{
630-
return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
631-
}
632-
633627
/* Encryption: only cache partial blocks
634628
* Decryption w/ padding: always keep at least one whole block
635629
* Decryption w/o padding: only cache partial blocks

tests/suites/helpers.function

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,10 @@ typedef enum
158158
} \
159159
while( 0 )
160160

161-
/** Allocate memory dynamically. Exit the test if this fails, but do
162-
* not mark the test as failed.
161+
/** Allocate memory dynamically. If the allocation fails, skip the test case.
163162
*
164163
* This macro behaves like #ASSERT_ALLOC, except that if the allocation
165-
* fails, it jumps to the \c exit label without calling test_fail().
164+
* fails, it marks the test as skipped rather than failed.
166165
*/
167166
#define ASSERT_ALLOC_WEAK( pointer, length ) \
168167
do \
@@ -172,8 +171,7 @@ typedef enum
172171
{ \
173172
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
174173
( length ) ); \
175-
if( ( pointer ) == NULL ) \
176-
goto exit; \
174+
TEST_ASSUME( ( pointer ) != NULL ); \
177175
} \
178176
} \
179177
while( 0 )

tests/suites/test_suite_asn1parse.function

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ int get_len_step( const data_t *input, size_t buffer_size,
121121
{
122122
unsigned char *buf = NULL;
123123
unsigned char *p = NULL;
124+
unsigned char *end;
124125
size_t parsed_length;
125126
int ret;
126127

@@ -130,7 +131,8 @@ int get_len_step( const data_t *input, size_t buffer_size,
130131
if( buffer_size == 0 )
131132
{
132133
ASSERT_ALLOC( buf, 1 );
133-
p = buf + 1;
134+
end = buf + 1;
135+
p = end;
134136
}
135137
else
136138
{
@@ -145,9 +147,10 @@ int get_len_step( const data_t *input, size_t buffer_size,
145147
memcpy( buf, input->x, buffer_size );
146148
}
147149
p = buf;
150+
end = buf + buffer_size;
148151
}
149152

150-
ret = mbedtls_asn1_get_len( &p, buf + buffer_size, &parsed_length );
153+
ret = mbedtls_asn1_get_len( &p, end, &parsed_length );
151154

152155
if( buffer_size >= input->len + actual_length )
153156
{

tests/suites/test_suite_ecdsa.function

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,9 @@ void ecdsa_write_restart( int id, char *d_str, int md_alg,
500500
TEST_ASSERT( md_info != NULL );
501501

502502
hlen = mbedtls_md_get_size( md_info );
503-
mbedtls_md( md_info, (const unsigned char *) msg, strlen( msg ), hash );
503+
TEST_ASSERT( mbedtls_md( md_info,
504+
(const unsigned char *) msg, strlen( msg ),
505+
hash ) == 0 );
504506

505507
mbedtls_ecp_set_max_ops( max_ops );
506508

tests/suites/test_suite_mpi.function

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@ void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
600600
TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
601601
TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
602602

603-
mbedtls_mpi_grow( &X, size_X );
604-
mbedtls_mpi_grow( &Y, size_Y );
603+
TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
604+
TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
605605

606606
TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
607607
if( input_err == 0 )

tests/suites/test_suite_pk.function

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,9 @@ void pk_sign_verify_restart( int pk_type, int grp_id, char *d_str,
844844
TEST_ASSERT( md_info != NULL );
845845

846846
hlen = mbedtls_md_get_size( md_info );
847-
mbedtls_md( md_info, (const unsigned char *) msg, strlen( msg ), hash );
847+
TEST_ASSERT( mbedtls_md( md_info,
848+
(const unsigned char *) msg, strlen( msg ),
849+
hash ) == 0 );
848850

849851
mbedtls_ecp_set_max_ops( max_ops );
850852

0 commit comments

Comments
 (0)