Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recvmsg_uninit does not work with MSG_TRUNC #1153

Closed
andylizi opened this issue Sep 5, 2024 · 0 comments · Fixed by #1159
Closed

recvmsg_uninit does not work with MSG_TRUNC #1153

andylizi opened this issue Sep 5, 2024 · 0 comments · Fixed by #1159

Comments

@andylizi
Copy link

andylizi commented Sep 5, 2024

recvfrom(2) has a MSG_TRUNC flag that makes it "return the real length of the packet or datagram, even when it was longer than the passed buffer."

This leads to panic since rustix::net::recvmsg_uninit tries to split the return buffer according to the returned length.

// Pass in a really small buffer
let (buf, _, _) = rustix::net::recvfrom_uninit(&socket, &mut buf, RecvFlags::TRUNC)?;
thread 'main' panicked at ~/.cargo/git/checkouts/rustix-8fafe793515fde97/9b432db/src/buffer.rs:18:30:
mid > len
stack backtrace:
   2: core::slice::<impl [T]>::split_at_mut
             at /rustc/0d634185dfddefe09047881175f35c65d68dcff1/library/core/src/slice/mod.rs:1906:21
   3: rustix::buffer::split_init
             at ~/.cargo/git/checkouts/rustix-8fafe793515fde97/9b432db/src/buffer.rs:18:26
   4: rustix::net::send_recv::recvfrom_uninit
             at ~/.cargo/git/checkouts/rustix-8fafe793515fde97/9b432db/src/net/send_recv/mod.rs:169:35

rustix::net::recvfrom doesn't suffer from this issue since it just returns the length directly. But it also doesn't support uninit buffers.

sunfishcode added a commit that referenced this issue Sep 9, 2024
With the `TRUNC` flag, `recv` returns the untruncated length, which may
be longer than the provided buffer. Fix `recv_uninit` and
`recvfrom_uninit` to handle this case.

This mean that `recv_uninit` and `recvfrom_uninit` are unable to return
the full untruncated length returned by `recv` with the `TRUNC` flag,
however it appears that fixing that may require API changes.

Fixes #1153.
sunfishcode added a commit that referenced this issue Sep 11, 2024
* Fix `recv_uninit` to handle the `TRUNC` flag.

With the `TRUNC` flag, `recv` returns the untruncated length, which may
be longer than the provided buffer. Fix `recv_uninit` and
`recvfrom_uninit` to handle this case.

This mean that `recv_uninit` and `recvfrom_uninit` are unable to return
the full untruncated length returned by `recv` with the `TRUNC` flag,
however it appears that fixing that may require API changes.

Fixes #1153.

* Add comments mentioning alternatives.
sunfishcode added a commit that referenced this issue Sep 11, 2024
* Fix `recv_uninit` to handle the `TRUNC` flag.

With the `TRUNC` flag, `recv` returns the untruncated length, which may
be longer than the provided buffer. Fix `recv_uninit` and
`recvfrom_uninit` to handle this case.

This mean that `recv_uninit` and `recvfrom_uninit` are unable to return
the full untruncated length returned by `recv` with the `TRUNC` flag,
however it appears that fixing that may require API changes.

Fixes #1153.

* Add comments mentioning alternatives.
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 a pull request may close this issue.

1 participant