-
-
Notifications
You must be signed in to change notification settings - Fork 2k
uucore: fix OsStr byte conversions on wasip2 and wasip3 #13755
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
Open
GamePad64
wants to merge
2
commits into
uutils:main
Choose a base branch
from
GamePad64:uucore-wasi-non-p1-osstr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+54
−35
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,8 +151,6 @@ use std::io::{BufRead, BufReader}; | |
| use std::iter; | ||
| #[cfg(unix)] | ||
| use std::os::unix::ffi::{OsStrExt, OsStringExt}; | ||
| #[cfg(all(target_os = "wasi", target_env = "p1"))] | ||
| use std::os::wasi::ffi::{OsStrExt, OsStringExt}; | ||
| use std::str; | ||
| use std::str::Utf8Chunk; | ||
| use std::sync::{LazyLock, atomic::Ordering}; | ||
|
|
@@ -485,15 +483,19 @@ pub fn os_str_as_bytes_lossy(os_string: &OsStr) -> Cow<'_, [u8]> { | |
| /// | ||
| /// This always succeeds on unix platforms, | ||
| /// and fails on other platforms if the bytes can't be parsed as UTF-8. | ||
| #[cfg_attr( | ||
| any(unix, all(target_os = "wasi", target_env = "p1")), | ||
| expect(clippy::unnecessary_wraps) | ||
| )] | ||
| #[cfg_attr(any(unix, target_os = "wasi"), expect(clippy::unnecessary_wraps))] | ||
| pub fn os_str_from_bytes(bytes: &[u8]) -> error::UResult<Cow<'_, OsStr>> { | ||
| #[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))] | ||
| #[cfg(unix)] | ||
| return Ok(Cow::Borrowed(OsStr::from_bytes(bytes))); | ||
|
|
||
| #[cfg(not(any(unix, all(target_os = "wasi", target_env = "p1"))))] | ||
| #[cfg(target_os = "wasi")] | ||
| return Ok(Cow::Borrowed( | ||
| // SAFETY: on WASI `OsStr` is a plain byte string with no encoding | ||
| // invariant, so every byte sequence is a valid `OsStr`. | ||
| unsafe { OsStr::from_encoded_bytes_unchecked(bytes) }, | ||
| )); | ||
|
|
||
| #[cfg(not(any(unix, target_os = "wasi")))] | ||
| Ok(Cow::Owned(OsString::from(str::from_utf8(bytes).map_err( | ||
| |_| error::UUsageError::new(1, "Unable to transform bytes into OsStr"), | ||
| )?))) | ||
|
|
@@ -503,15 +505,17 @@ pub fn os_str_from_bytes(bytes: &[u8]) -> error::UResult<Cow<'_, OsStr>> { | |
| /// | ||
| /// This always succeeds on unix platforms, | ||
| /// and fails on other platforms if the bytes can't be parsed as UTF-8. | ||
| #[cfg_attr( | ||
| any(unix, all(target_os = "wasi", target_env = "p1")), | ||
| expect(clippy::unnecessary_wraps) | ||
| )] | ||
| #[cfg_attr(any(unix, target_os = "wasi"), expect(clippy::unnecessary_wraps))] | ||
| pub fn os_string_from_vec(vec: Vec<u8>) -> error::UResult<OsString> { | ||
| #[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))] | ||
| #[cfg(unix)] | ||
| return Ok(OsString::from_vec(vec)); | ||
|
|
||
| #[cfg(not(any(unix, all(target_os = "wasi", target_env = "p1"))))] | ||
| #[cfg(target_os = "wasi")] | ||
| // SAFETY: on WASI `OsString` is a plain byte string with no encoding | ||
| // invariant, so every byte sequence is a valid `OsString`. | ||
| return Ok(unsafe { OsString::from_encoded_bytes_unchecked(vec) }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we avoid the unsafe ? |
||
|
|
||
| #[cfg(not(any(unix, target_os = "wasi")))] | ||
| Ok(OsString::from(String::from_utf8(vec).map_err(|_| { | ||
| error::UUsageError::new(1, "invalid UTF-8 was detected in one or more arguments") | ||
| })?)) | ||
|
|
@@ -521,14 +525,13 @@ pub fn os_string_from_vec(vec: Vec<u8>) -> error::UResult<OsString> { | |
| /// | ||
| /// This always succeeds on unix platforms, | ||
| /// and fails on other platforms if the bytes can't be parsed as UTF-8. | ||
| #[cfg_attr( | ||
| any(unix, all(target_os = "wasi", target_env = "p1")), | ||
| expect(clippy::unnecessary_wraps) | ||
| )] | ||
| #[cfg_attr(any(unix, target_os = "wasi"), expect(clippy::unnecessary_wraps))] | ||
| pub fn os_string_to_vec(s: OsString) -> error::UResult<Vec<u8>> { | ||
| #[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))] | ||
| #[cfg(unix)] | ||
| let v = s.into_vec(); | ||
| #[cfg(not(any(unix, all(target_os = "wasi", target_env = "p1"))))] | ||
| #[cfg(target_os = "wasi")] | ||
| let v = s.into_encoded_bytes(); | ||
| #[cfg(not(any(unix, target_os = "wasi")))] | ||
| let v = s | ||
| .into_string() | ||
| .map_err(|_| { | ||
|
|
@@ -760,6 +763,35 @@ mod tests { | |
| test_invalid_utf8_args_ignore(os_str); | ||
| } | ||
|
|
||
| /// On byte-oriented platforms — unix and every WASI environment — the | ||
| /// `OsString` helpers must round-trip arbitrary bytes, including sequences | ||
| /// that are not valid UTF-8. This regressed on wasm32-wasip2 and | ||
| /// wasm32-wasip3, where the conversions fell back to the UTF-8-validating | ||
| /// path and returned an error instead. | ||
| #[cfg(any(unix, target_os = "wasi"))] | ||
| #[test] | ||
| fn os_string_from_vec_roundtrips_non_utf8() { | ||
| let source = vec![0x66, 0x6f, 0x80, 0x6f]; | ||
|
|
||
| let os_string = os_string_from_vec(source.clone()).unwrap(); | ||
| // The value really does hold bytes that are not valid UTF-8. | ||
| assert!(os_string.to_str().is_none()); | ||
| assert_eq!(os_string_to_vec(os_string).unwrap(), source); | ||
| } | ||
|
|
||
| /// Same invariant for the borrowed `OsStr` conversions, which must agree | ||
| /// with `os_string_from_vec` on what is representable. | ||
| #[cfg(any(unix, target_os = "wasi"))] | ||
| #[test] | ||
| fn os_str_from_bytes_roundtrips_non_utf8() { | ||
| let source = [0x66, 0x6f, 0x80, 0x6f]; | ||
|
|
||
| let os_str = os_str_from_bytes(&source).unwrap(); | ||
| assert!(os_str.to_str().is_none()); | ||
| assert_eq!(os_str_as_bytes(&os_str).unwrap(), &source); | ||
| assert_eq!(os_str_as_bytes_lossy(&os_str).into_owned(), source); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_format_usage() { | ||
| assert_eq!(format_usage("expr EXPRESSION"), "expr EXPRESSION"); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for the unsafe