Skip to content

Fix minor defects found by Coverity #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions library/cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i

*olen = 0;
block_size = mbedtls_cipher_get_block_size( ctx );
if ( 0 == block_size )
{
return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
}

if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
{
Expand Down Expand Up @@ -562,11 +566,6 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
}
#endif

if ( 0 == block_size )
{
return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
}

if( input == output &&
( ctx->unprocessed_len != 0 || ilen % block_size ) )
{
Expand Down Expand Up @@ -625,11 +624,6 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
*/
if( 0 != ilen )
{
if( 0 == block_size )
{
return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
}

/* Encryption: only cache partial blocks
* Decryption w/ padding: always keep at least one whole block
* Decryption w/o padding: only cache partial blocks
Expand Down
8 changes: 3 additions & 5 deletions tests/suites/helpers.function
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,10 @@ typedef enum
} \
while( 0 )

/** Allocate memory dynamically. Exit the test if this fails, but do
* not mark the test as failed.
/** Allocate memory dynamically. If the allocation fails, skip the test case.
*
* This macro behaves like #ASSERT_ALLOC, except that if the allocation
* fails, it jumps to the \c exit label without calling test_fail().
* fails, it marks the test as skipped rather than failed.
*/
#define ASSERT_ALLOC_WEAK( pointer, length ) \
do \
Expand All @@ -172,8 +171,7 @@ typedef enum
{ \
( pointer ) = mbedtls_calloc( sizeof( *( pointer ) ), \
( length ) ); \
if( ( pointer ) == NULL ) \
goto exit; \
TEST_ASSUME( ( pointer ) != NULL ); \
} \
} \
while( 0 )
Expand Down
7 changes: 5 additions & 2 deletions tests/suites/test_suite_asn1parse.function
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ int get_len_step( const data_t *input, size_t buffer_size,
{
unsigned char *buf = NULL;
unsigned char *p = NULL;
unsigned char *end;
size_t parsed_length;
int ret;

Expand All @@ -130,7 +131,8 @@ int get_len_step( const data_t *input, size_t buffer_size,
if( buffer_size == 0 )
{
ASSERT_ALLOC( buf, 1 );
p = buf + 1;
end = buf + 1;
p = end;
}
else
{
Expand All @@ -145,9 +147,10 @@ int get_len_step( const data_t *input, size_t buffer_size,
memcpy( buf, input->x, buffer_size );
}
p = buf;
end = buf + buffer_size;
}

ret = mbedtls_asn1_get_len( &p, buf + buffer_size, &parsed_length );
ret = mbedtls_asn1_get_len( &p, end, &parsed_length );

if( buffer_size >= input->len + actual_length )
{
Expand Down
4 changes: 3 additions & 1 deletion tests/suites/test_suite_ecdsa.function
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,9 @@ void ecdsa_write_restart( int id, char *d_str, int md_alg,
TEST_ASSERT( md_info != NULL );

hlen = mbedtls_md_get_size( md_info );
mbedtls_md( md_info, (const unsigned char *) msg, strlen( msg ), hash );
TEST_ASSERT( mbedtls_md( md_info,
(const unsigned char *) msg, strlen( msg ),
hash ) == 0 );

mbedtls_ecp_set_max_ops( max_ops );

Expand Down
4 changes: 2 additions & 2 deletions tests/suites/test_suite_mpi.function
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,8 @@ void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );

mbedtls_mpi_grow( &X, size_X );
mbedtls_mpi_grow( &Y, size_Y );
TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );

TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
if( input_err == 0 )
Expand Down
4 changes: 3 additions & 1 deletion tests/suites/test_suite_pk.function
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,9 @@ void pk_sign_verify_restart( int pk_type, int grp_id, char *d_str,
TEST_ASSERT( md_info != NULL );

hlen = mbedtls_md_get_size( md_info );
mbedtls_md( md_info, (const unsigned char *) msg, strlen( msg ), hash );
TEST_ASSERT( mbedtls_md( md_info,
(const unsigned char *) msg, strlen( msg ),
hash ) == 0 );

mbedtls_ecp_set_max_ops( max_ops );

Expand Down