Skip to content

Commit 4edbee2

Browse files
committed
Safely copy GID
Signed-off-by: Yadunund <[email protected]>
1 parent cd27cb5 commit 4edbee2

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,16 @@ rmw_ret_t PublisherData::publish(
289289
const size_t data_length = ser.get_serialized_data_length();
290290

291291
z_owned_bytes_map_t map =
292-
create_map_and_set_sequence_num(sequence_number_++, gid_);
292+
create_map_and_set_sequence_num(
293+
sequence_number_++,
294+
[this](z_owned_bytes_map_t * map, const char * key)
295+
{
296+
// Mutex already locked.
297+
z_bytes_t gid_bytes;
298+
gid_bytes.len = RMW_GID_STORAGE_SIZE;
299+
gid_bytes.start = gid_;
300+
z_bytes_map_insert_by_copy(map, z_bytes_new(key), gid_bytes);
301+
});
293302
if (!z_check(map)) {
294303
// create_map_and_set_sequence_num already set the error
295304
return RMW_RET_ERROR;
@@ -341,8 +350,16 @@ rmw_ret_t PublisherData::publish_serialized_message(
341350

342351
std::lock_guard<std::mutex> lock(mutex_);
343352

344-
z_owned_bytes_map_t map =
345-
rmw_zenoh_cpp::create_map_and_set_sequence_num(sequence_number_++, gid_);
353+
z_owned_bytes_map_t map = rmw_zenoh_cpp::create_map_and_set_sequence_num(
354+
sequence_number_++,
355+
[this](z_owned_bytes_map_t * map, const char * key)
356+
{
357+
// Mutex already locked.
358+
z_bytes_t gid_bytes;
359+
gid_bytes.len = RMW_GID_STORAGE_SIZE;
360+
gid_bytes.start = gid_;
361+
z_bytes_map_insert_by_copy(map, z_bytes_new(key), gid_bytes);
362+
});
346363

347364
if (!z_check(map)) {
348365
// create_map_and_set_sequence_num already set the error
@@ -391,10 +408,10 @@ liveliness::TopicInfo PublisherData::topic_info() const
391408
}
392409

393410
///=============================================================================
394-
const uint8_t * PublisherData::gid() const
411+
void PublisherData::copy_gid(rmw_gid_t * gid) const
395412
{
396413
std::lock_guard<std::mutex> lock(mutex_);
397-
return gid_;
414+
memcpy(gid->data, gid_, RMW_GID_STORAGE_SIZE);
398415
}
399416

400417
///=============================================================================

rmw_zenoh_cpp/src/detail/rmw_publisher_data.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class PublisherData final
7070
// Get a copy of the TopicInfo of this PublisherData.
7171
liveliness::TopicInfo topic_info() const;
7272

73-
// Get the GID of this PublisherData.
74-
const uint8_t * gid() const;
73+
// Copy the GID of this PublisherData into an rmw_gid_t.
74+
void copy_gid(rmw_gid_t * gid) const;
7575

7676
// Returns true if liveliness token is still valid.
7777
bool liveliness_is_valid() const;

rmw_zenoh_cpp/src/detail/zenoh_utils.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ namespace rmw_zenoh_cpp
2525
{
2626
///=============================================================================
2727
z_owned_bytes_map_t
28-
create_map_and_set_sequence_num(int64_t sequence_number, const uint8_t gid[RMW_GID_STORAGE_SIZE])
28+
create_map_and_set_sequence_num(
29+
int64_t sequence_number,
30+
GIDCopier gid_copier)
2931
{
3032
z_owned_bytes_map_t map = z_bytes_map_new();
3133
if (!z_check(map)) {
@@ -54,12 +56,7 @@ create_map_and_set_sequence_num(int64_t sequence_number, const uint8_t gid[RMW_G
5456
return z_bytes_map_null();
5557
}
5658
z_bytes_map_insert_by_copy(&map, z_bytes_new("source_timestamp"), z_bytes_new(source_ts_str));
57-
58-
z_bytes_t gid_bytes;
59-
gid_bytes.len = RMW_GID_STORAGE_SIZE;
60-
gid_bytes.start = gid;
61-
62-
z_bytes_map_insert_by_copy(&map, z_bytes_new("source_gid"), gid_bytes);
59+
gid_copier(&map, "source_gid");
6360

6461
free_attachment_map.cancel();
6562

rmw_zenoh_cpp/src/detail/zenoh_utils.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717

1818
#include <zenoh.h>
1919

20+
#include <functional>
21+
2022
#include "rmw/types.h"
2123

2224
namespace rmw_zenoh_cpp
2325
{
2426
///=============================================================================
27+
// A function to safely copy an entity's GID as a z_bytes_t into a
28+
// z_owned_bytes_map_t for a given key.
29+
using GIDCopier = std::function<void (z_owned_bytes_map_t *, const char *)>;
30+
///=============================================================================
2531
z_owned_bytes_map_t
26-
create_map_and_set_sequence_num(int64_t sequence_number, const uint8_t gid[RMW_GID_STORAGE_SIZE]);
32+
create_map_and_set_sequence_num(int64_t sequence_number, GIDCopier gid_copier);
2733

2834
} // namespace rmw_zenoh_cpp
2935

rmw_zenoh_cpp/src/rmw_zenoh.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,13 @@ rmw_send_request(
20892089

20902090
z_owned_bytes_map_t map = rmw_zenoh_cpp::create_map_and_set_sequence_num(
20912091
*sequence_id,
2092-
client_data->client_gid);
2092+
[client_data](z_owned_bytes_map_t * map, const char * key)
2093+
{
2094+
z_bytes_t gid_bytes;
2095+
gid_bytes.len = RMW_GID_STORAGE_SIZE;
2096+
gid_bytes.start = client_data->client_gid;
2097+
z_bytes_map_insert_by_copy(map, z_bytes_new(key), gid_bytes);
2098+
});
20932099
if (!z_check(map)) {
20942100
// create_map_and_set_sequence_num already set the error
20952101
return RMW_RET_ERROR;
@@ -2781,7 +2787,14 @@ rmw_send_response(
27812787
z_query_reply_options_t options = z_query_reply_options_default();
27822788

27832789
z_owned_bytes_map_t map = rmw_zenoh_cpp::create_map_and_set_sequence_num(
2784-
request_header->sequence_number, request_header->writer_guid);
2790+
request_header->sequence_number,
2791+
[request_header](z_owned_bytes_map_t * map, const char * key)
2792+
{
2793+
z_bytes_t gid_bytes;
2794+
gid_bytes.len = RMW_GID_STORAGE_SIZE;
2795+
gid_bytes.start = request_header->writer_guid;
2796+
z_bytes_map_insert_by_copy(map, z_bytes_new(key), gid_bytes);
2797+
});
27852798
if (!z_check(map)) {
27862799
// create_map_and_set_sequence_num already set the error
27872800
return RMW_RET_ERROR;
@@ -3438,7 +3451,7 @@ rmw_get_gid_for_publisher(const rmw_publisher_t * publisher, rmw_gid_t * gid)
34383451
RMW_CHECK_ARGUMENT_FOR_NULL(pub_data, RMW_RET_INVALID_ARGUMENT);
34393452

34403453
gid->implementation_identifier = rmw_zenoh_cpp::rmw_zenoh_identifier;
3441-
memcpy(gid->data, pub_data->gid(), RMW_GID_STORAGE_SIZE);
3454+
pub_data->copy_gid(gid);
34423455

34433456
return RMW_RET_OK;
34443457
}

0 commit comments

Comments
 (0)