Skip to content

Commit

Permalink
Fixed an issue in listener.sctp_send
Browse files Browse the repository at this point in the history
The `listener.sctp_send` would return `EINVAL` when run as `cargo test
--release`, but not otherwise. This was related to using raw pointer to
a structure that went out of scope inside an `unsafe` block. Making sure
that the struct lives long enough for us to use it's raw pointer.
  • Loading branch information
gabhijit committed Nov 8, 2023
1 parent e3047ee commit b14c935
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
1 change: 0 additions & 1 deletion sctp-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ os_socketaddr = { version = "0.2" }
libc = { version = "0.2" }
tokio = { version = "1.0" , features = ["net", "macros", "rt"]}
log = { version = "0.4" }
hex = "*"

[dev-dependencies]
clap = "4"
Expand Down
17 changes: 9 additions & 8 deletions sctp-rs/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ pub(crate) async fn sctp_recvmsg_internal(
msg_iov: &mut recv_iov,
msg_iovlen: 1,
msg_control: msg_control.as_mut_ptr() as *mut _ as *mut libc::c_void,
msg_controllen: msg_controllen,
msg_controllen,
msg_flags: 0,
};

Expand Down Expand Up @@ -586,12 +586,13 @@ pub(crate) async fn sctp_sendmsg_internal(
iov_len: data.payload.len(),
};

// We have to create this `os_sockaddr` outside the `if let ...`
// Else it will go out of scope and we'll be using it's raw pointer.
let os_sockaddr: OsSocketAddr;
let (to_buffer, to_buffer_len) = if let Some(addr) = to {
let os_sockaddr: OsSocketAddr = addr.into();
(
os_sockaddr.as_ptr() as *mut libc::c_void,
os_sockaddr.capacity(),
)
os_sockaddr = addr.into();
let slice: &[u8] = os_sockaddr.as_ref();
(slice.as_ptr() as *mut _, os_sockaddr.len())
} else {
(std::ptr::null::<OsSocketAddr>() as *mut libc::c_void, 0)
};
Expand All @@ -616,7 +617,7 @@ pub(crate) async fn sctp_sendmsg_internal(
let msg_controllen = msg_control_size as u32;

#[cfg(not(target_os = "macos"))]
let msg_controllen = msg_control_size as usize;
let msg_controllen = msg_control_size;

let mut sendmsg_header = libc::msghdr {
msg_name: to_buffer,
Expand All @@ -640,7 +641,7 @@ pub(crate) async fn sctp_sendmsg_internal(
let snd_info = data.snd_info.unwrap();
std::ptr::copy(
std::ptr::addr_of!(snd_info) as *const _,
libc::CMSG_DATA(cmsg_hdr) as *mut _ as *mut u8,
libc::CMSG_DATA(cmsg_hdr),
std::mem::size_of::<SendInfo>(),
);
}
Expand Down

0 comments on commit b14c935

Please sign in to comment.