Skip to content

Handle non-NUL-terminated strings in SocketAddrUnix. #1371

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

Merged
merged 11 commits into from
Mar 3, 2025

Conversation

sunfishcode
Copy link
Member

Unix-domain socket address can be long enough that the NUL terminator does not fit. Handle this case by making path() return a Cow<CStr> and adding a NUL terminator as needed.

Also add a path_bytes() function for returning the raw bytes.

Fixes #1316.

Unix-domain socket address can be long enough that the NUL terminator does
not fit. Handle this case by making `path()` return a `Cow<CStr>` and
adding a NUL terminator as needed.

Also add a `path_bytes()` function for returning the raw bytes.

Fixes #1316.
@Kijewski
Copy link
Contributor

Kijewski commented Mar 3, 2025

Would it maybe be possible to make the array one element longer instead?

@sunfishcode
Copy link
Member Author

We define SocketAddrUnix to have the same layout as sockaddr_un, so we can't easily change the size of the sun_path field.

Using `.to_owned()` + `.push()` will cause a reallocation, because the
initially allocated array with be one byte too short.

We can use `CString::from_vec_with_nul_unchecked()` because it is a
known invariant that the input does not contain any `NUL`s, not even the
terminating `NUL`.
@sunfishcode sunfishcode merged commit 4ad9705 into main Mar 3, 2025
51 checks passed
@sunfishcode sunfishcode deleted the sunfishcode/nonterminated-sun-path branch March 3, 2025 19:21
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.

Panic on Unix-domain sockets with length 108
2 participants