From dfc95ade620952af587dc55464d0697efad15301 Mon Sep 17 00:00:00 2001 From: yuanyuyuan Date: Sun, 18 Aug 2024 22:18:54 +0800 Subject: [PATCH] fix: segmentation fault due to the unallocated query memory --- rmw_zenoh_cpp/src/detail/rmw_data_types.cpp | 6 +++--- rmw_zenoh_cpp/src/detail/rmw_data_types.hpp | 2 +- rmw_zenoh_cpp/src/rmw_init.cpp | 2 -- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 21 +++++++-------------- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/rmw_data_types.cpp b/rmw_zenoh_cpp/src/detail/rmw_data_types.cpp index d39084ae..0d288964 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_data_types.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_data_types.cpp @@ -465,15 +465,15 @@ void sub_data_handler(const z_loaned_sample_t *sample, void *data) { ///============================================================================= ZenohQuery::ZenohQuery(const z_loaned_query_t *query) { - z_query_clone(query_, query); + z_query_clone(&query_, query); } ///============================================================================= -ZenohQuery::~ZenohQuery() { z_drop(z_move(*query_)); } +ZenohQuery::~ZenohQuery() { z_drop(z_move(query_)); } ///============================================================================= const z_loaned_query_t *ZenohQuery::get_query() const { - return z_query_loan(query_); + return z_query_loan(&query_); } //============================================================================== diff --git a/rmw_zenoh_cpp/src/detail/rmw_data_types.hpp b/rmw_zenoh_cpp/src/detail/rmw_data_types.hpp index 0b314d35..b6744971 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_data_types.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_data_types.hpp @@ -216,7 +216,7 @@ class ZenohQuery final const z_loaned_query_t * get_query() const; private: - z_owned_query_t * query_{nullptr}; + z_owned_query_t query_; }; ///============================================================================= diff --git a/rmw_zenoh_cpp/src/rmw_init.cpp b/rmw_zenoh_cpp/src/rmw_init.cpp index 66f67b67..2a8f8772 100644 --- a/rmw_zenoh_cpp/src/rmw_init.cpp +++ b/rmw_zenoh_cpp/src/rmw_init.cpp @@ -386,8 +386,6 @@ rmw_ret_t rmw_init(const rmw_init_options_t *options, rmw_context_t *context) { free_shm_provider.cancel(); restore_context.cancel(); - - return RMW_RET_OK; } diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index 26689c5f..662ff494 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -1416,17 +1416,14 @@ rmw_subscription_t *rmw_create_subscription( sub_options.reliability = Z_RELIABILITY_RELIABLE; } - ; - - ze_owned_querying_subscriber_t *sub = - &std::get(sub_data->sub); - ze_declare_querying_subscriber(sub, z_loan(context_impl->session), + ze_owned_querying_subscriber_t sub; + if (ze_declare_querying_subscriber(&sub, z_loan(context_impl->session), z_loan(keyexpr), z_move(callback), - &sub_options); - if (!z_check(*sub)) { + &sub_options)) { RMW_SET_ERROR_MSG("unable to create zenoh subscription"); return nullptr; } + sub_data->sub = sub; } else { // Create a regular subscriber for all other durability settings. z_subscriber_options_t sub_options; @@ -1435,13 +1432,12 @@ rmw_subscription_t *rmw_create_subscription( sub_options.reliability = Z_RELIABILITY_RELIABLE; } - z_owned_subscriber_t *sub = &std::get(sub_data->sub); - z_declare_subscriber(sub, z_loan(context_impl->session), z_loan(keyexpr), - z_move(callback), &sub_options); - if (!z_check(*sub)) { + z_owned_subscriber_t sub; + if(z_declare_subscriber(&sub, z_loan(context_impl->session), z_loan(keyexpr), z_move(callback), &sub_options)) { RMW_SET_ERROR_MSG("unable to create zenoh subscription"); return nullptr; } + sub_data->sub = sub; } auto undeclare_z_sub = rcpputils::make_scope_exit([sub_data]() { @@ -2374,9 +2370,6 @@ rmw_ret_t rmw_send_request(const rmw_client_t *client, const void *ros_request, z_owned_closure_reply_t callback; z_closure(&callback, rmw_zenoh_cpp::client_data_handler, NULL, client_data); - - // TODO(yuyuan): z_owned_closure_reply_t zn_closure_reply is replaced with a - // moved callback z_get(z_loan(context_impl->session), z_loan(client_data->keyexpr), "", z_move(callback), &opts);