Skip to content

Commit 6c47883

Browse files
get_all_data_impl() does not handle null pointers properly, causing segmentation fault (#2840)
* get_all_data_impl() does not handle null pointers properly, causing segmentation fault Signed-off-by: Alejandro Hernandez Cordero <[email protected]> * Update rclcpp/include/rclcpp/experimental/buffers/ring_buffer_implementation.hpp Co-authored-by: Janosch Machowinski <[email protected]> Signed-off-by: Alejandro Hernández Cordero <[email protected]> * make linters happy Signed-off-by: Alejandro Hernandez Cordero <[email protected]> --------- Signed-off-by: Alejandro Hernandez Cordero <[email protected]> Signed-off-by: Alejandro Hernández Cordero <[email protected]> Co-authored-by: Janosch Machowinski <[email protected]>
1 parent 73e9bfb commit 6c47883

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

rclcpp/include/rclcpp/experimental/buffers/ring_buffer_implementation.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,13 @@ class RingBufferImplementation : public BufferImplementationBase<BufferT>
265265
std::vector<BufferT> result_vtr;
266266
result_vtr.reserve(size_);
267267
for (size_t id = 0; id < size_; ++id) {
268-
result_vtr.emplace_back(
269-
new typename is_std_unique_ptr<T>::Ptr_type(
270-
*(ring_buffer_[(read_index_ + id) % capacity_])));
268+
const auto & elem(ring_buffer_[(read_index_ + id) % capacity_]);
269+
if (elem != nullptr) {
270+
result_vtr.emplace_back(new typename is_std_unique_ptr<T>::Ptr_type(
271+
*elem));
272+
} else {
273+
result_vtr.emplace_back(nullptr);
274+
}
271275
}
272276
return result_vtr;
273277
}

rclcpp/test/rclcpp/test_ring_buffer_implementation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,15 @@ TEST(TestRingBufferImplementation, test_buffer_clear) {
164164
EXPECT_EQ('c', c);
165165
EXPECT_EQ('d', d);
166166
}
167+
168+
TEST(TestRingBufferImplementation, handle_nullptr_deletion) {
169+
rclcpp::experimental::buffers::RingBufferImplementation<std::unique_ptr<int>> rb(3);
170+
rb.enqueue(std::make_unique<int>(42));
171+
rb.enqueue(nullptr); // intentionally enqueuing nullptr
172+
rb.enqueue(std::make_unique<int>(84));
173+
auto all_data = rb.get_all_data();
174+
EXPECT_EQ(3u, all_data.size());
175+
EXPECT_EQ(42, *(all_data[0]));
176+
EXPECT_EQ(nullptr, all_data[1]);
177+
EXPECT_EQ(84, *(all_data[2]));
178+
}

0 commit comments

Comments
 (0)