Skip to content

Commit

Permalink
Prevent MsgHdr::with_control with empty buffer causes error in macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Apr 22, 2024
1 parent 6923954 commit 53a293b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,11 @@ impl<'addr, 'bufs, 'control> MsgHdr<'addr, 'bufs, 'control> {
/// Corresponds to setting `msg_control` and `msg_controllen` on Unix and
/// `Control` on Windows.
pub fn with_control(mut self, buf: &'control [u8]) -> Self {
let ptr = buf.as_ptr() as *mut _;
let ptr = if buf.is_empty() {
std::ptr::null_mut()
} else {
buf.as_ptr() as *mut _
};
sys::set_msghdr_control(&mut self.inner, ptr, buf.len());
self
}
Expand Down
5 changes: 4 additions & 1 deletion tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,10 @@ fn sendmsg() {

let bufs = &[IoSlice::new(DATA)];
let addr_b = socket_b.local_addr().unwrap();
let msg = socket2::MsgHdr::new().with_addr(&addr_b).with_buffers(bufs);
let msg = socket2::MsgHdr::new()
.with_addr(&addr_b)
.with_buffers(bufs)
.with_control(&[]);
let sent = socket_a.sendmsg(&msg, 0).unwrap();
assert_eq!(sent, DATA.len());

Expand Down

0 comments on commit 53a293b

Please sign in to comment.