Fix the fix to ssl_set_psk()

- possible for the first malloc to fail and the second to succeed
- missing = NULL assignment
This commit is contained in:
Manuel Pégourié-Gonnard 2015-02-18 10:23:52 +00:00
parent df4e44025d
commit f45850c493

View file

@ -4052,26 +4052,23 @@ int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
if( psk_len > POLARSSL_PSK_MAX_LEN )
return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
if( ssl->psk != NULL )
if( ssl->psk != NULL || ssl->psk_identity != NULL )
{
polarssl_free( ssl->psk );
polarssl_free( ssl->psk_identity );
}
ssl->psk_len = psk_len;
ssl->psk_identity_len = psk_identity_len;
ssl->psk = polarssl_malloc( ssl->psk_len );
ssl->psk_identity = polarssl_malloc( ssl->psk_identity_len );
if( ssl->psk == NULL )
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
if( ssl->psk_identity == NULL )
if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
{
polarssl_free( ssl->psk );
ssl->psk = NULL;
return( POLARSSL_ERR_SSL_MALLOC_FAILED );
}
ssl->psk_len = psk_len;
ssl->psk_identity_len = psk_identity_len;
memcpy( ssl->psk, psk, ssl->psk_len );
memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );