Repository navigation
Conversation
libwebrtc passes a zero-length message with a null pointer, and slice::from_raw_parts requires a non-null pointer even for len 0, so a debug build aborts on any empty message a peer sends. Treat len 0 as an empty slice.
|
|
| let data = if buffer.len == 0 { | ||
| &[][..] | ||
| } else { | ||
| unsafe { std::slice::from_raw_parts(buffer.ptr, buffer.len) } |
There was a problem hiding this comment.
🟡 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
@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.
|
Closing in favor of #1698 |
libwebrtc delivers a zero-length data channel message with a null pointer.
DataChannelObserverWrapper::on_messagepasses it tostd::slice::from_raw_parts, which requires a non-null pointer even forlen == 0. So a debug build aborts the whole process (unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null) as soon as any peer sends an empty message. Release builds keep going, though it is still UB.This treats
len == 0as an empty slice and only callsfrom_raw_partsotherwise.We hit this on a robot station that receives teleoperation commands over Cloudflare Realtime data channels: a single empty packet from a remote peer took the debug station down.