Fix memory allocation fail in TCP mock socket
Because two buffers were aliased too early in the code, it was possible that
after an allocation failure, free() would be called twice for the same pointer.
@@ -224,34 +224,36 @@ int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
{
int ret = -1;
- peer1->input = peer2->output =
+ peer1->output =
(mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
- if( peer1->input == NULL )
+ if( peer1->output == NULL )
{
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
goto exit;
}
- mbedtls_test_buffer_init( peer1->input );
- if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->input, bufsize ) ) )
+ mbedtls_test_buffer_init( peer1->output );
+ if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
{
goto exit;
}
- peer1->output = peer2->input =
+ peer2->output =
(mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
- if( peer1->output == NULL )
+ if( peer2->output == NULL )
{
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
goto exit;
}
- mbedtls_test_buffer_init( peer1->output );
- if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
+ mbedtls_test_buffer_init( peer2->output );
+ if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
{
goto exit;
}
peer1->peer = peer2;
peer2->peer = peer1;
+ peer1->input = peer2->output;
+ peer2->input = peer1->output;
peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
ret = 0;
