Skip to content

Commit

Permalink
Fix memory leak when UnsubscribePacket is reused (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfod authored May 1, 2024
1 parent 414d8c5 commit e092720
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion source/mqtt/Mqtt5Packets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace Aws
const Crt::Vector<String> &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)
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
28 changes: 28 additions & 0 deletions tests/Mqtt5ClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mqtt5::Mqtt5Client> mqtt5Client = Mqtt5::Mqtt5Client::NewMqtt5Client(mqtt5Options, allocator);
ASSERT_TRUE(mqtt5Client);

Vector<String> unsubList{TEST_TOPIC};
std::shared_ptr<Mqtt5::UnsubscribePacket> unsubscribe = std::make_shared<Mqtt5::UnsubscribePacket>(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]
//////////////////////////////////////////////////////////
Expand Down

0 comments on commit e092720

Please sign in to comment.