From 24dabef278b12937908f91529d996f14feaa55df Mon Sep 17 00:00:00 2001 From: yijiem Date: Mon, 16 Sep 2024 14:44:33 -0700 Subject: [PATCH] fix memory leak --- .../alts/frame_protector/alts_frame_protector.cc | 1 + .../frame_protector/alts_frame_protector_test.cc | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/core/tsi/alts/frame_protector/alts_frame_protector.cc b/src/core/tsi/alts/frame_protector/alts_frame_protector.cc index 09415c413d6..c5acd50921b 100644 --- a/src/core/tsi/alts/frame_protector/alts_frame_protector.cc +++ b/src/core/tsi/alts/frame_protector/alts_frame_protector.cc @@ -380,6 +380,7 @@ tsi_result alts_create_frame_protector(const uint8_t* key, size_t key_size, if (status != GRPC_STATUS_OK) { LOG(ERROR) << "Failed to create ALTS crypters, " << error_details; gpr_free(error_details); + gpr_free(impl); return TSI_INTERNAL_ERROR; } /// diff --git a/test/core/tsi/alts/frame_protector/alts_frame_protector_test.cc b/test/core/tsi/alts/frame_protector/alts_frame_protector_test.cc index e513caf0678..2ebb9bc69ba 100644 --- a/test/core/tsi/alts/frame_protector/alts_frame_protector_test.cc +++ b/test/core/tsi/alts/frame_protector/alts_frame_protector_test.cc @@ -402,6 +402,19 @@ TEST(AltsFrameProtectorTest, MainTest) { alts_test_do_round_trip_all(/*rekey=*/true); } +TEST(AltsFrameProtectorTest, MemoryLeakTest) { + tsi_frame_protector* client_frame_protector = nullptr; + // Create a key with a wrong key length (off-by-one). + uint8_t* key = nullptr; + size_t key_length = kAes128GcmKeyLength - 1; + gsec_test_random_array(&key, key_length); + EXPECT_EQ(alts_create_frame_protector(key, key_length, /*is_client=*/true, + /*rekey=*/false, nullptr, + &client_frame_protector), + TSI_INTERNAL_ERROR); + gpr_free(key); +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();