Skip to content

webrtc-sys: accept an empty data channel message - #1630

Closed
sutyum wants to merge 2 commits into
livekit:mainfrom
Sentient-X:webrtc-sys-empty-data-channel-message
Closed

sutyum wants to merge 2 commits into
livekit:mainfrom
Sentient-X:webrtc-sys-empty-data-channel-message

Conversation

@sutyum

@sutyum sutyum commented Nov 3, 2026

Copy link
Copy Markdown

libwebrtc delivers a zero-length data channel message with a null pointer. DataChannelObserverWrapper::on_message passes it to std::slice::from_raw_parts, which requires a non-null pointer even for len == 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 == 0 as an empty slice and only calls from_raw_parts otherwise.

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.

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.
@sutyum
sutyum requested a review from ladvoc as a code owner November 3, 2026 20:51
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@devin-ai-integration devin-ai-integration Bot left a comment

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.

Devin Review found 1 potential issue.

Devin Review

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.

@ladvoc

ladvoc commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Closing in favor of #1698

@ladvoc ladvoc closed this Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants