Skip to content

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

Merged
sunfishcode merged 11 commits intomainfrom
sunfishcode/nonterminated-sun-path
Mar 3, 2025
Merged

Handle non-NUL-terminated strings in SocketAddrUnix.#1371
sunfishcode merged 11 commits intomainfrom
sunfishcode/nonterminated-sun-path

Conversation

@sunfishcode
Copy link
Copy Markdown
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
Copy Markdown
Contributor

Kijewski commented Mar 3, 2025

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

@sunfishcode
Copy link
Copy Markdown
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
@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