From 28fe0fc1cc27de2ee3cd858538649be43482958e Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Thu, 11 Apr 2024 10:24:12 -0400 Subject: [PATCH] Fix uninitialized memory in test When I added in the tests for large messages, I made a mistake and reserved space in the strings, but didn't actually expand it. Thus, we were writing into uninitialized memory. Fix this by just using the correct constructor for string, which will allocate and initialize the memory properly. Signed-off-by: Chris Lalancette --- rclcpp/test/rclcpp/test_publisher_with_type_adapter.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/rclcpp/test/rclcpp/test_publisher_with_type_adapter.cpp b/rclcpp/test/rclcpp/test_publisher_with_type_adapter.cpp index 9367a89014..d70b0e382d 100644 --- a/rclcpp/test/rclcpp/test_publisher_with_type_adapter.cpp +++ b/rclcpp/test/rclcpp/test_publisher_with_type_adapter.cpp @@ -379,9 +379,7 @@ TEST_F(TestPublisher, test_large_message_unique) auto pub = node->create_publisher(topic_name, 1); static constexpr size_t length = 10 * 1024 * 1024; - auto message_data = std::make_unique(); - message_data->reserve(length); - std::fill(message_data->begin(), message_data->begin() + length, '#'); + auto message_data = std::make_unique(length, '#'); pub->publish(std::move(message_data)); } @@ -400,8 +398,6 @@ TEST_F(TestPublisher, test_large_message_constref) auto pub = node->create_publisher(topic_name, 1); static constexpr size_t length = 10 * 1024 * 1024; - std::string message_data; - message_data.reserve(length); - std::fill(message_data.begin(), message_data.begin() + length, '#'); + std::string message_data(length, '#'); pub->publish(message_data); }