Skip to content

Commit

Permalink
Explicitly ensure subscription depth is minimum 1
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Jan 23, 2024
1 parent e0a1de1 commit 5314de6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions rmw_zenoh_cpp/src/detail/rmw_data_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ void sub_data_handler(
sub_data->adapted_qos_profile.depth,
z_loan(keystr));

std::unique_ptr<saved_msg_data> old = std::move(sub_data->message_queue.front());
z_drop(&old->payload);
sub_data->message_queue.pop_front();
// If the adapted_qos_profile.depth is 0, the std::move command below will result
// in UB and the z_drop will segfault. We explicitly set the depth to a minimum of 1
// in rmw_create_subscription() but to be safe, we only attempt to discard from the
// queue if it is non-empty.
if (!sub_data->message_queue.empty()) {
std::unique_ptr<saved_msg_data> old = std::move(sub_data->message_queue.front());
z_drop(&old->payload);
sub_data->message_queue.pop_front();
}
}

sub_data->message_queue.emplace_back(
Expand Down
4 changes: 4 additions & 0 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,10 @@ rmw_create_subscription(
RMW_SET_ERROR_MSG("Failed to obtain adapted_qos_profile.");
return nullptr;
}
// Explicitly ensure the history depth is at least 1.
sub_data->adapted_qos_profile.depth = std::max(
static_cast<std::size_t>(1),
sub_data->adapted_qos_profile.depth);

sub_data->typesupport_identifier = type_support->typesupport_identifier;
sub_data->type_support_impl = type_support->data;
Expand Down

0 comments on commit 5314de6

Please sign in to comment.