Skip to content

Commit

Permalink
[#500] Pass destination buffer size when retrieving ids
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Nov 13, 2024
1 parent a646c13 commit 499107e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
8 changes: 4 additions & 4 deletions iceoryx2-ffi/cxx/src/unique_port_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ UniquePublisherId::UniquePublisherId(iox2_unique_publisher_id_h handle)
auto UniquePublisherId::bytes() -> iox::optional<RawIdType>& {
if (!m_raw_id.has_value() && m_handle != nullptr) {
RawIdType bytes { UNIQUE_PORT_ID_LENGTH, 0 };
iox2_unique_publisher_id_value(m_handle, bytes.data());
iox2_unique_publisher_id_value(m_handle, bytes.data(), bytes.size());
m_raw_id.emplace(std::move(bytes));
}
return m_raw_id;
Expand Down Expand Up @@ -93,7 +93,7 @@ UniqueSubscriberId::UniqueSubscriberId(iox2_unique_subscriber_id_h handle)
auto UniqueSubscriberId::bytes() -> iox::optional<RawIdType>& {
if (!m_raw_id.has_value() && m_handle != nullptr) {
RawIdType bytes { UNIQUE_PORT_ID_LENGTH, 0 };
iox2_unique_subscriber_id_value(m_handle, bytes.data());
iox2_unique_subscriber_id_value(m_handle, bytes.data(), bytes.size());
m_raw_id.emplace(std::move(bytes));
}
return m_raw_id;
Expand Down Expand Up @@ -139,7 +139,7 @@ UniqueNotifierId::UniqueNotifierId(iox2_unique_notifier_id_h handle)
auto UniqueNotifierId::bytes() -> iox::optional<RawIdType>& {
if (!m_raw_id.has_value() && m_handle != nullptr) {
RawIdType bytes { UNIQUE_PORT_ID_LENGTH, 0 };
iox2_unique_notifier_id_value(m_handle, bytes.data());
iox2_unique_notifier_id_value(m_handle, bytes.data(), bytes.size());
m_raw_id.emplace(std::move(bytes));
}
return m_raw_id;
Expand Down Expand Up @@ -185,7 +185,7 @@ UniqueListenerId::UniqueListenerId(iox2_unique_listener_id_h handle)
auto UniqueListenerId::bytes() -> iox::optional<RawIdType>& {
if (!m_raw_id.has_value() && m_handle != nullptr) {
RawIdType bytes { UNIQUE_PORT_ID_LENGTH, 0 };
iox2_unique_listener_id_value(m_handle, bytes.data());
iox2_unique_listener_id_value(m_handle, bytes.data(), bytes.size());
m_raw_id.emplace(std::move(bytes));
}
return m_raw_id;
Expand Down
19 changes: 18 additions & 1 deletion iceoryx2-ffi/ffi/src/api/unique_listener_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,37 @@ impl HandleToType for iox2_unique_listener_id_h_ref {

// BEGIN C API

/// Retrieves the value of a unique listener ID.
///
/// # Arguments
///
/// * `handle` - A valid [`iox2_unique_listener_id_h`]
/// * `id_ptr` - Pointer to a buffer where the ID value will be written
/// * `id_length` - The length of the buffer pointed to by `id_ptr`
///
/// # Safety
///
/// * `handle` must be a valid, non-null pointer
/// * `id_ptr` must be a valid, non-null pointer to a buffer of at least `id_length` bytes
/// * `id_length` must be large enough to hold the ID value
#[no_mangle]
unsafe extern "C" fn iox2_unique_listener_id_value(
handle: iox2_unique_listener_id_h,
id_ptr: *mut u8,
id_length: usize,
) {
debug_assert!(!id_ptr.is_null());
handle.assert_non_null();

let h = &mut *handle.as_type();

if let Some(Some(id)) = (h.value.internal.as_ptr() as *const Option<UniqueListenerId>).as_ref()
{
let bytes = id.value().to_ne_bytes();
debug_assert!(bytes.len() <= id_length, "id_length is too small");

unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, bytes.len());
std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, id_length);
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion iceoryx2-ffi/ffi/src/api/unique_notifier_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,37 @@ impl HandleToType for iox2_unique_notifier_id_h_ref {

// BEGIN C API

/// Retrieves the value of a unique notifier ID.
///
/// # Arguments
///
/// * `handle` - A valid [`iox2_unique_notifier_id_h`]
/// * `id_ptr` - Pointer to a buffer where the ID value will be written
/// * `id_length` - The length of the buffer pointed to by `id_ptr`
///
/// # Safety
///
/// * `handle` must be a valid, non-null pointer
/// * `id_ptr` must be a valid, non-null pointer to a buffer of at least `id_length` bytes
/// * `id_length` must be large enough to hold the ID value
#[no_mangle]
unsafe extern "C" fn iox2_unique_notifier_id_value(
handle: iox2_unique_notifier_id_h,
id_ptr: *mut u8,
id_length: usize,
) {
debug_assert!(!id_ptr.is_null());
handle.assert_non_null();

let h = &mut *handle.as_type();

if let Some(Some(id)) = (h.value.internal.as_ptr() as *const Option<UniqueNotifierId>).as_ref()
{
let bytes = id.value().to_ne_bytes();
debug_assert!(bytes.len() <= id_length, "id_length is too small");

unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, bytes.len());
std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, id_length);
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion iceoryx2-ffi/ffi/src/api/unique_publisher_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,37 @@ impl HandleToType for iox2_unique_publisher_id_h_ref {

// BEGIN C API

/// Retrieves the value of a unique publisher ID.
///
/// # Arguments
///
/// * `handle` - A valid [`iox2_unique_publisher_id_h`]
/// * `id_ptr` - Pointer to a buffer where the ID value will be written
/// * `id_length` - The length of the buffer pointed to by `id_ptr`
///
/// # Safety
///
/// * `handle` must be a valid, non-null pointer
/// * `id_ptr` must be a valid, non-null pointer to a buffer of at least `id_length` bytes
/// * `id_length` must be large enough to hold the ID value
#[no_mangle]
unsafe extern "C" fn iox2_unique_publisher_id_value(
handle: iox2_unique_publisher_id_h,
id_ptr: *mut u8,
id_length: usize,
) {
debug_assert!(!id_ptr.is_null());
handle.assert_non_null();

let h = &mut *handle.as_type();

if let Some(Some(id)) = (h.value.internal.as_ptr() as *const Option<UniquePublisherId>).as_ref()
{
let bytes = id.value().to_ne_bytes();
debug_assert!(bytes.len() <= id_length, "id_length is too small");

unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, bytes.len());
std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, id_length);
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion iceoryx2-ffi/ffi/src/api/unique_subscriber_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,26 @@ impl HandleToType for iox2_unique_subscriber_id_h_ref {

// BEGIN C API

/// Retrieves the value of a unique subscriber ID.
///
/// # Arguments
///
/// * `handle` - A valid [`iox2_unique_subscriber_id_h`]
/// * `id_ptr` - Pointer to a buffer where the ID value will be written
/// * `id_length` - The length of the buffer pointed to by `id_ptr`
///
/// # Safety
///
/// * `handle` must be a valid, non-null pointer
/// * `id_ptr` must be a valid, non-null pointer to a buffer of at least `id_length` bytes
/// * `id_length` must be large enough to hold the ID value
#[no_mangle]
unsafe extern "C" fn iox2_unique_subscriber_id_value(
handle: iox2_unique_subscriber_id_h,
id_ptr: *mut u8,
id_length: usize,
) {
debug_assert!(!id_ptr.is_null());
handle.assert_non_null();

let h = &mut *handle.as_type();
Expand All @@ -99,8 +114,10 @@ unsafe extern "C" fn iox2_unique_subscriber_id_value(
(h.value.internal.as_ptr() as *const Option<UniqueSubscriberId>).as_ref()
{
let bytes = id.value().to_ne_bytes();
debug_assert!(bytes.len() <= id_length, "id_length is too small");

unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, bytes.len());
std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, id_length);
}
}
}
Expand Down

0 comments on commit 499107e

Please sign in to comment.