Skip to content

Conversation

thaliaarchi
Copy link
Contributor

Motor OS (a new target added in #147000) guarantees that strings from the OS are valid UTF-8, yet the OsStrExt/OsStringExt traits, which aim to expose the inner UTF-8 representations, use checked UTF-8 conversions.

The only way for a user to construct an OsStr/OsString from arbitrary bytes is via from_encoded_bytes_unchecked, which requires:

As the encoding is unspecified, callers must pass in bytes that originated as a mixture of validated UTF-8 and bytes from OsStr::as_encoded_bytes from within the same Rust version built for the same target platform.

Thus, callers are required to supply bytes which originated from OsStr::as_encoded_bytes (i.e., guaranteed to be UTF-8) and/or are valid UTF-8, so it is library UB for an OsStr/OsString to contain invalid UTF-8 on Motor OS.

Since the standard library can make these guarantees, I think it is appropriate for these extension traits to perform unchecked conversions. As they stand, they offer no benefit over existing methods.

Also: Replace OsStringExt::as_str with OsStringExt::into_string (just use OsStr::as_str for the other) and mirror the Unix comments for these functions.

cc @lasiotus

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 17, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 17, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants