Closed japaric closed 1 year ago
Note that as presented here, I would go for the second approach, but also consider using a version without any unsafe
in it. My rationale:
unsafe
appears, i.e. both pieces of code look just as (un)safe to me and then I would prefer the more optimal one.unsafe
block in both versions are not really necessary and could be replaced by unwrap
s (and then again I would prefer the more-optimal one).I like this idea and I think we should implement it, I don't have a strong opinion about either implementation so I'd check how often would we call as_cstr
and as_str
and pick the implementation which is more efficient for the most commonly called method.
Is this related to #213? Does it subsume it?
I think this issue is a particular subset of #213 as the latter also discuss things like Path
and OsString
. To be honest it would be nice to have a type that's also a OsString
and a CString
at the same time as there are some paths that we use with the standard library interface and with a C interface.
there are a few places in the codebase where Rust
str
ings are converted intoCStr
prior to calling libc functions (example below).The conversion is runtime checked because
str
can contain null bytes whereasCStr
cannot. Some of thestr
ings subjected to these conversions cannot contain null bytes because, e.g., they come from the command line interface and due to the wayexec*
functions work command line arguments cannot contain null bytes.So one could envision a newtype that allows infallible conversion to both
str
andCStr
This could be used for example in the
User
struct: https://github.com/memorysafety/sudo-rs/blob/9a7f38fbddc59f40a5e0a57555131b36e09811e4/src/system/mod.rs#L260The goal would be to push the runtime check / validation towards the "edge" of sudo-rs, e.g. where CLI parsing happens, and then avoid further runtime checks in the rest of the pipeline.
Alternatively, one could push a null byte into the
str
when creating aSudoString
to reduce allocations -- at the cost of adding moreunsafe
blocks.