Uh oh!
There was an error while loading. Please reload this page.
Allow limited access to OsStr bytes - #109698
Conversation
`OsStr` has historically kept its implementation details private out of concern for locking us into a specific encoding on Windows. This is an alternative to rust-lang#95290 which proposed specifying the encoding on Windows. Instead, this only specifies that for cross-platform code, `OsStr`'s encoding is a superset of UTF-8 and defines rules for safely interacting with it At minimum, this can greatly simplify the `os_str_bytes` crate and every arg parser that interacts with `OsStr` directly (which is most of those that support invalid UTF-8).
rustbot
commented
Mar 28, 2023
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cuviper (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
rustbot
commented
Mar 28, 2023
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
01f8d93 to
0d87d66Comparecuviper
commented
Mar 28, 2023
I think this counts as new guarantee for API purposes. @rustbot label +T-libs-api -T-libs |
ChrisDenton
commented
Mar 28, 2023
Out of interest, was there anything in particular that motivated you to propose this now? |
the8472
commented
Mar 28, 2023
I think this should come with an example since it's a wide pointer. Conversion from C to Rust has to jump through extra hoops. |
joshtriplett
commented
Mar 29, 2023
I'm personally amenable to adding this guarantee, but I do agree with @the8472 that we need to confirm that this will actually work as intended. |
OsStrOsStr bytes in unsafe blocksSorry for the lack of details and confusion on this. I was under the impression that working towards my final goal would best be done in smaller steps of defining My hope that this more limited alternative to #95290 will have a chance to move forward. |
ChrisDenton
commented
Mar 31, 2023
If we do this, I wonder if it makes sense to also offer a // Similar to `str::split_at` and `[T]::split_at`// Panics if `mid` is not on a UTF-8 code point boundary.fnsplit_at(&self,mid:usize) -> (&OsStr,&OsStr);Splitting and joining are the two hazard areas when dealing with known valid |
A part of me finds it weird to offer a safe function like EDIT: To make clear, I'm open to adding that function if that is the direction we want to take this. |
ChrisDenton
commented
Mar 31, 2023
Hm, we could offer some safe way to get an index according to these rules. E.g. // Returns the byte index where `searcher` first returns true.// `searcher` can inspect `char`s decoded from UTF-8.// Non UTF-8 encoded characters are skipped.fnfind_char(&self,searcher:FnMut(char) -> bool) -> Option<usize>;But at this point I'll stop because I'm practically designing yet another alternative approach. |
chorman0773
commented
Apr 11, 2023
Does this confirm that |
For now, this proposals restricts what is safe to transmute from //! - When [transmuting] from `&[u8]` to `&OsStr`,//! - the slice may only include content from comparable `&OsStr` (see above) or be valid UTF-8//! - any splits of the `&OsStr` must be along char boundaries (the first byte of a UTF-8 code//! point sequence) |
BurntSushi
commented
Apr 11, 2023
I think I buy the rules as written, but I would like to see a doc example using Also, cc @SimonSapin to see what you think about this. (Let me know if you want me to stop pinging you about this, but I always think of you as the champion against a change like this.) Popping up a level, this is kind of an interesting use of |
blyxxyz
commented
Apr 11, 2023
It actually isn't a bag of bytes on Windows, it must be WTF-8-encoded or you can start reading out of bounds: fnmain(){let b:&[u8] = b"\xC2";let s:&std::ffi::OsStr = unsafe{ std::mem::transmute(b)};dbg!(s);}$ cargo run -q --target x86_64-pc-windows-gnu[src/main.rs:4] s = "\u{9b}:] = \n\0\0\0\0\0\0\01\u{10}L[...]thread 'main' panicked at 'failed printing to stderr: Windows stdio in console mode does not support writing non-UTF-8 byte sequences', library\std\src\io\stdio.rs:1008:9The proposal is phrased to never produce invalid WTF-8. I think that as written now it allows for any |
This wording would provide an additional guarantee that |
e2d912b to
e6a35c4Compareepage
commented
May 30, 2023
I've addressed what caused the wasm build failure and it should be good to go again. |
Amanieu
commented
May 30, 2023
@bors r+ |
bors
commented
May 30, 2023
Allow limited access to `OsStr` bytes `OsStr` has historically kept its implementation details private out of concern for locking us into a specific encoding on Windows. This is an alternative to rust-lang#95290 which proposed specifying the encoding on Windows. Instead, this only specifies that for cross-platform code, `OsStr`'s encoding is a superset of UTF-8 and defines rules for safely interacting with it At minimum, this can greatly simplify the `os_str_bytes` crate and every arg parser that interacts with `OsStr` directly (which is most of those that support invalid UTF-8). Tracking issue: rust-lang#111544
bors
commented
May 30, 2023
bors
commented
May 31, 2023
☀️ Test successful - checks-actions |
rust-timer
commented
May 31, 2023
Finished benchmarking commit (9610dfe): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 642.846s -> 643.715s (0.14%) |
This extends rust-lang#109698 to allow no-cost conversion between `Vec<u8>` and `OsString` as suggested in feedback from `os_str_bytes` crate in rust-lang#111544.
Allow limited access to `OsString` bytes This extends rust-lang#109698 to allow no-cost conversion between `Vec<u8>` and `OsString` as suggested in feedback from `os_str_bytes` crate in rust-lang#111544.
Allow limited access to `OsString` bytes This extends rust-lang#109698 to allow no-cost conversion between `Vec<u8>` and `OsString` as suggested in feedback from `os_str_bytes` crate in rust-lang#111544.
Allow limited access to `OsString` bytes This extends rust-lang#109698 to allow no-cost conversion between `Vec<u8>` and `OsString` as suggested in feedback from `os_str_bytes` crate in rust-lang#111544.
OsStrhas historically kept its implementation details private out ofconcern for locking us into a specific encoding on Windows.
This is an alternative to #95290 which proposed specifying the encoding on Windows. Instead, this
only specifies that for cross-platform code,
OsStr's encoding is a superset of UTF-8 and definesrules for safely interacting with it
At minimum, this can greatly simplify the
os_str_bytescrate and everyarg parser that interacts with
OsStrdirectly (which is most of thosethat support invalid UTF-8).
Tracking issue: #111544