Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
webrtc-sys: patch
---

An empty data channel message no longer aborts a debug build: libwebrtc passes it with a null pointer, which `slice::from_raw_parts` refuses, so a zero-length message is now an empty slice.
12 changes: 8 additions & 4 deletions webrtc-sys/src/data_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ impl DataChannelObserverWrapper {
}

fn on_message(&self, buffer: ffi::DataBuffer) {
unsafe {
let data = std::slice::from_raw_parts(buffer.ptr, buffer.len);
self.observer.on_message(data, buffer.binary);
}
// libwebrtc hands an empty message over with a null pointer, which
// `slice::from_raw_parts` does not accept (a debug build aborts on it).
let data = if buffer.len == 0 {
&[][..]
} else {
unsafe { std::slice::from_raw_parts(buffer.ptr, buffer.len) }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Missing safety rationale for native buffer

For nonempty messages, on_message constructs a slice without the required // SAFETY: comment. The block needs to explain the pointer's validity and lifetime.

Learn more

The repository safety rules require every unsafe block to have a // SAFETY: comment explaining why the operation is safe. The null-pointer explanation covers empty messages but does not establish the pointer validity or lifetime for nonempty messages. NativeDataChannelObserver::OnMessage passes the pointer and size from a WebRTC buffer during a synchronous callback.

Example: For a nonempty packet, buffer.ptr and buffer.len come from the native buffer; the raw slice must remain valid through the observer call.

Recommended fix: Add a // SAFETY: comment directly above the unsafe block stating why the nonempty buffer pointer is valid, adequately aligned, and remains alive for the callback.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@ladvoc ladvoc Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sutyum, thank you for your contribution! Please add a brief // SAFETY: comment on the unsafe block itself to satisfy the repository requirement. Other than that, this change looks good.

};
self.observer.on_message(data, buffer.binary);
}

fn on_buffered_amount_change(&self, sent_data_size: u64) {
Expand Down
Loading