-
Notifications
You must be signed in to change notification settings - Fork 675
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
Make uname
always safe
#1672
Make uname
always safe
#1672
Conversation
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.
Do you know of any cases where the strings aren't valid UTF-8 ? It is certainly more convenient for the user if we return a &str
than a &[u8]
. Perhaps do a checked conversion to utf8?
Not specifically, but e.g. someone could set a non-UTF8 hostname with
Well, I see three possibilities here:
Converting the |
What if it returns an |
|
LGTM; can you squash and rebase? |
This fixes several issues with the current `uname` bindings: - Do not ignore `uname` errors; at least on glibc `uname` can fail, so now it returns a `Result` instead of assuming that the call will always succeed. - Do not assume `uname` will initialize every member of `utsname`; not every implementation initializes every field, so internally the struct is now zero-initialized. - Do not blindly assume strings returned by `uname` will always be valid UTF-8; `UtsName`'s accessors will now return `&OsStr`s instead of `&str`s.
Done! |
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.
bors r+
Currently
uname
doesn't check for errors and just blindly assumes that it always succeeds. According to the manpage this function can fail, even though no actual errors are defined:Looking at the glibc's sources we can see that it indeed could fail if the internal
gethostname
call fails for some reason.This code also assumes that every field of
utsname
is going to be initialized by the call touname
, which apparently is also not true. Even though the interface doesn't expose this field so it's not a problem in practice (although it might be UB since we do callassume_init
on the whole struct) theutsname
does have adomainname
field which glibc doesn't initialize.The code also assumes that every field is a valid UTF-8 string, which is also technically not guaranteed.
The code also assumes that every field will be null terminated, which might not be true if any of the strings are too long (since glibc uses
strncpy
which will not null-terminate the string if it ends up running out of space).This PR should fix all of these problems.
This is a breaking change.