-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
OsStrExt3::from_bytes
should be unsafe
#1594
Comments
Thank you for the detailed report! Without having looking into each use, I think this stems from workarounds that no longer need to exist. I.e. much of what clap needs is just sequences of bytes and not UTF-8 encoded strings. Originally this meant shoehorning I'll start looking deeper and see if I can come up with a list, including the locations you mentioned, that we should look into fix/replacing. As far as Windows is concerned, I think this will require some sort of |
I'll happily provide you a list later on. Would you still be interested in a PR that cleans that up a bit short-term? Theres one thing I want to point out which I don't fully understand, maybe you can shed some light (and maybe it's a point of note anyways): How is this safe? You are making That said, because of this everything will panic in the non-utf8 case on windows anyways, so maybe we should embrace this for now? |
We have quite a few levels of abstraction here, so I'll try to go through each one and explain why I think this issue might be a problem in practice.
The above holds if truly arbitrary byte sequences are allowed to enter |
Upon further discussion, since this applies to |
Hi! I created OsStr Bytes a while ago, but today I removed all unsafe code. This issue came up on that thread, so I was wondering if you would be open to a PR that uses this crate to perform the conversions. If so, there are some design questions I have about the implementation:
Would this be something you'd want to see added? |
@dylni ya sure, you can go ahead and make a PR :) Safety comes first |
Great :) I'm a bit busy recently, but I'll send a PR when I can find some time. Plus, that will give this time to gather opinions on implementation. |
That works for us :) Thanks |
@dylni We are totally open to
|
@CreepySkeleton Thanks for the responses and interest! I don't expect the performance hit to be large, but I am interested to see how measurable it is. Some basic testing has shown the methods to be as fast as the standard library's implementation. |
I'm opening this as a result from rust-secure-code/safety-dance#49. The mentioned method uses
unsafe
, but is not markedunsafe
itself. I think that is not right, and while I've found no safety violations here, there's the possibility of a future bug in safe code if the necessary properties aren't upheld. @Shnatsel remarkedand while I don't understand the details of this claim, it's probably worth looking at.
There's some prior art at #1054 and #1058, but the issue of
from_bytes
was left without action. @H2CO3 saidI'm uncertain about this, as far as I can see, this implementation is only valid on systems where
OsStr
is an arbitrary byte sequence, e.g. linux, but not on Windows, whereOsStr
is backed by a struct calledWtf8
, where the corresponding method is marked unsafe indeed.My takeaway is that this seems like an error we should fix. There are quite some usages which are basically trivial to fix by introducing a
from_str
method onOsStr
, like here. There are other usages though which seem harder to fix since slicing anOsStr
at arbitray indices doe not work on windows. As far as I can see those usages are bugs and will turn into problems when called with the "right" arguments. They are all inosstringext.rs
, so maybe they can be fixed without any change outside that file.I'm open to sending a PR, but I'd need some guidance on what seems acceptable to you. Also, I'm on linux, so I probably can't run proper benchmarks to see if the changes have a bad effect (note: That's just for
osstringext.rs
, a potentialfrom_str
method should be doable without any performance loss).The text was updated successfully, but these errors were encountered: