diff --git a/source/mqtt/Mqtt5Packets.cpp b/source/mqtt/Mqtt5Packets.cpp index b9300e956..c578af921 100644 --- a/source/mqtt/Mqtt5Packets.cpp +++ b/source/mqtt/Mqtt5Packets.cpp @@ -138,7 +138,7 @@ namespace Aws const Crt::Vector &stringVector, Allocator *allocator) { - AWS_ZERO_STRUCT(dst); + aws_array_list_clean_up(&dst); if (aws_array_list_init_dynamic(&dst, allocator, stringVector.size(), sizeof(aws_byte_cursor)) != AWS_OP_SUCCESS) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 41e048923..d0576813b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -235,6 +235,7 @@ if(NOT BYO_CRYPTO) add_net_test_case(Mqtt5NullPublish) add_net_test_case(Mqtt5NullSubscribe) add_net_test_case(Mqtt5NullUnsubscribe) + add_net_test_case(Mqtt5ReuseUnsubscribePacket) add_net_test_case(Mqtt5QoS1SubPub) add_net_test_case(Mqtt5RetainSetAndClear) diff --git a/tests/Mqtt5ClientTest.cpp b/tests/Mqtt5ClientTest.cpp index 4ba308d43..e2a390c0f 100644 --- a/tests/Mqtt5ClientTest.cpp +++ b/tests/Mqtt5ClientTest.cpp @@ -2285,6 +2285,34 @@ static int s_TestMqtt5NullUnsubscribe(Aws::Crt::Allocator *allocator, void *) } AWS_TEST_CASE(Mqtt5NullUnsubscribe, s_TestMqtt5NullUnsubscribe) +/* + * Reuse unsubscribe packet test. + * The scenario in this test once caused memory leak, so the test ensures the issue is fixed for good. + */ +static int s_TestMqtt5ReuseUnsubscribePacket(Aws::Crt::Allocator *allocator, void *) +{ + ApiHandle apiHandle(allocator); + + const String TEST_TOPIC = "test/s_TestMqtt5NullUnsubscribe" + Aws::Crt::UUID().ToString(); + + Mqtt5::Mqtt5ClientOptions mqtt5Options(allocator); + mqtt5Options.WithHostName("www.example.com").WithPort(1111); + std::shared_ptr mqtt5Client = Mqtt5::Mqtt5Client::NewMqtt5Client(mqtt5Options, allocator); + ASSERT_TRUE(mqtt5Client); + + Vector unsubList{TEST_TOPIC}; + std::shared_ptr unsubscribe = std::make_shared(allocator); + unsubscribe->WithTopicFilters(unsubList); + ASSERT_TRUE(mqtt5Client->Unsubscribe(unsubscribe)); + /* Unsubscribe once again using the same UnsubscribePacket. */ + ASSERT_TRUE(mqtt5Client->Unsubscribe(unsubscribe)); + + ASSERT_TRUE(mqtt5Client->Stop()); + + return AWS_OP_SUCCESS; +} +AWS_TEST_CASE(Mqtt5ReuseUnsubscribePacket, s_TestMqtt5ReuseUnsubscribePacket) + ////////////////////////////////////////////////////////// // QoS1 Test Cases [QoS1-UC] //////////////////////////////////////////////////////////