From 039e009faacf35fccde1414dff1a0c55fe3d8aba Mon Sep 17 00:00:00 2001 From: Matthew Stevenson Date: Thu, 11 Jun 2020 19:21:51 -0700 Subject: [PATCH] Fix memory leak, per Yang's comment. --- src/core/tsi/ssl_transport_security.cc | 13 +++++-------- .../tests/unit/_server_ssl_cert_config_test.py | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/core/tsi/ssl_transport_security.cc b/src/core/tsi/ssl_transport_security.cc index c013690fbd8..476aec47144 100644 --- a/src/core/tsi/ssl_transport_security.cc +++ b/src/core/tsi/ssl_transport_security.cc @@ -1299,7 +1299,7 @@ static const tsi_handshaker_result_vtable handshaker_result_vtable = { }; static tsi_result ssl_handshaker_result_create( - tsi_ssl_handshaker* handshaker, const unsigned char* unused_bytes, + tsi_ssl_handshaker* handshaker, unsigned char* unused_bytes, size_t unused_bytes_size, tsi_handshaker_result** handshaker_result) { if (handshaker == nullptr || handshaker_result == nullptr || (unused_bytes_size > 0 && unused_bytes == nullptr)) { @@ -1313,11 +1313,8 @@ static tsi_result ssl_handshaker_result_create( handshaker->ssl = nullptr; result->network_io = handshaker->network_io; handshaker->network_io = nullptr; - if (unused_bytes_size > 0) { - result->unused_bytes = - static_cast(gpr_malloc(unused_bytes_size)); - memcpy(result->unused_bytes, unused_bytes, unused_bytes_size); - } + /* Transfer ownership of |unused_bytes| to the handshaker result. */ + result->unused_bytes = unused_bytes; result->unused_bytes_size = unused_bytes_size; *handshaker_result = &result->base; return TSI_OK; @@ -1425,8 +1422,8 @@ static tsi_result ssl_bytes_remaining(tsi_ssl_handshaker* impl, size_t bytes_in_ssl = BIO_pending(SSL_get_rbio(impl->ssl)); if (bytes_in_ssl == 0) return TSI_OK; *bytes_remaining = static_cast(gpr_malloc(bytes_in_ssl)); - int bytes_read = - BIO_read(SSL_get_rbio(impl->ssl), *bytes_remaining, bytes_in_ssl); + int bytes_read = BIO_read(SSL_get_rbio(impl->ssl), *bytes_remaining, + static_cast(bytes_in_ssl)); // If an unexpected number of bytes were read, return an error status and free // all of the bytes that were read. if (bytes_read < 0 || static_cast(bytes_read) != bytes_in_ssl) { diff --git a/src/python/grpcio_tests/tests/unit/_server_ssl_cert_config_test.py b/src/python/grpcio_tests/tests/unit/_server_ssl_cert_config_test.py index 958baf99805..35d992a33d6 100644 --- a/src/python/grpcio_tests/tests/unit/_server_ssl_cert_config_test.py +++ b/src/python/grpcio_tests/tests/unit/_server_ssl_cert_config_test.py @@ -167,8 +167,8 @@ class _ServerSSLCertReloadTest( # the handshake is complete, so the TSI handshaker returns the # TSI_PROTOCOL_FAILURE result. This result does not have a # corresponding status code, so this yields an UNKNOWN status. - self.assertTrue(exception_context.exception.code() in - [grpc.StatusCode.UNAVAILABLE, grpc.StatusCode.UNKNOWN]) + self.assertTrue(exception_context.exception.code( + ) in [grpc.StatusCode.UNAVAILABLE, grpc.StatusCode.UNKNOWN]) def _do_one_shot_client_rpc(self, expect_success,