Skip to content

Allocated SocketAddrUnix::path() with correct length#1372

Merged
sunfishcode merged 1 commit intobytecodealliance:sunfishcode/nonterminated-sun-pathfrom
Kijewski:pr-path_with_termination
Mar 3, 2025
Merged

Allocated SocketAddrUnix::path() with correct length#1372
sunfishcode merged 1 commit intobytecodealliance:sunfishcode/nonterminated-sun-pathfrom
Kijewski:pr-path_with_termination

Conversation

@Kijewski
Copy link
Copy Markdown
Contributor

@Kijewski Kijewski commented Mar 3, 2025

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 NULs, not even the terminating NUL.


@sunfishcode, I hope it's okay that I post a PR to a PR. It was easier to implement the change in a commit than to add multiple suggestions in #1371.

@Kijewski Kijewski force-pushed the pr-path_with_termination branch 2 times, most recently from ae87066 to d183318 Compare March 3, 2025 17:57
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`.
@Kijewski Kijewski force-pushed the pr-path_with_termination branch from d183318 to 0d0ba07 Compare March 3, 2025 18:05
@sunfishcode
Copy link
Copy Markdown
Member

Cool, thanks!

@sunfishcode sunfishcode merged commit 4e2f0ac into bytecodealliance:sunfishcode/nonterminated-sun-path Mar 3, 2025
@Kijewski Kijewski deleted the pr-path_with_termination branch March 3, 2025 18:26
sunfishcode added a commit that referenced this pull request Mar 3, 2025
* Handle non-NUL-terminated strings in `SocketAddrUnix`.

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.

* Allocated `SocketAddrUnix::path()` with correct length (#1372)

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`.

---------

Co-authored-by: René Kijewski <Kijewski@users.noreply.github.com>
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.

2 participants