diff options
| author | Rusty Wagner <rusty.wagner@gmail.com> | 2025-12-23 13:12:02 -0700 |
|---|---|---|
| committer | Rusty Wagner <rusty.wagner@gmail.com> | 2026-05-22 16:30:56 -0400 |
| commit | 8d621c51b2797fda7b1dc22243dde611cfc04f68 (patch) | |
| tree | ba5e6a90e644d21d13e75dabcbd0cc747444443a /rust/src/ffi.rs | |
| parent | 08e34ac325743085911f96b62c81d9a1f2127806 (diff) | |
Refactor calling conventions to support correct representation of structures
Diffstat (limited to 'rust/src/ffi.rs')
| -rw-r--r-- | rust/src/ffi.rs | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/rust/src/ffi.rs b/rust/src/ffi.rs index 45d142bf..aaa4851e 100644 --- a/rust/src/ffi.rs +++ b/rust/src/ffi.rs @@ -31,6 +31,19 @@ pub(crate) fn time_from_bn(timestamp: u64) -> SystemTime { UNIX_EPOCH + m } +pub(crate) unsafe fn slice_from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] { + if len == 0 { + // C can and will pass null pointers for data in the case of zero length arrays. + // According to the documentation of std::slice::from_raw_parts, data must be + // non-null and properly aligned. To avoid creating unsound slices, return an + // empty slice directly on any zero-length array, avoiding the unsound call + // to std::slice::from_raw_parts. + &[] + } else { + unsafe { std::slice::from_raw_parts(data, len) } + } +} + #[macro_export] macro_rules! ffi_span { ($name:expr, $bv:expr) => {{ |
