-
Notifications
You must be signed in to change notification settings - Fork 1.2k
cygwin: fix off-by-one in utsname.sysname length #5099
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
phdye
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
phdye-cygwin-rust:cygwin-utsname-sysname-65
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.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| //! Verify that `libc::uname()` populates the `utsname` struct with each field | ||
| //! starting at the correct C offset on Cygwin. | ||
| //! | ||
| //! Cygwin's `<sys/utsname.h>` declares all six fields as 65 bytes | ||
| //! (`_UTSNAME_LENGTH`). If any field is mis-sized in the Rust declaration, | ||
| //! every subsequent field shifts in the struct and `CStr::from_ptr` reads | ||
| //! that field starting at the wrong byte. The most observable consequence | ||
| //! is that `nodename` no longer matches `gethostname()`, so this test is | ||
| //! the smallest behavioural check that catches such a regression. | ||
|
|
||
| #![cfg(target_os = "cygwin")] | ||
|
|
||
| use std::ffi::CStr; | ||
| use std::mem::MaybeUninit; | ||
|
|
||
| #[test] | ||
| fn uname_nodename_matches_gethostname() { | ||
| let mut uts = MaybeUninit::<libc::utsname>::uninit(); | ||
| let mut host = [0u8; 256]; | ||
|
|
||
| unsafe { | ||
| assert_ne!( | ||
| libc::uname(uts.as_mut_ptr()), | ||
| -1, | ||
| "uname() failed", | ||
| ); | ||
| assert_ne!( | ||
| libc::gethostname(host.as_mut_ptr() as *mut libc::c_char, host.len()), | ||
| -1, | ||
| "gethostname() failed", | ||
| ); | ||
|
|
||
| let uts = uts.assume_init(); | ||
| let nodename = CStr::from_ptr(uts.nodename.as_ptr()) | ||
| .to_str() | ||
| .expect("uname.nodename is not valid UTF-8"); | ||
| let hostname = CStr::from_ptr(host.as_ptr() as *const libc::c_char) | ||
| .to_str() | ||
| .expect("gethostname() returned non-UTF-8"); | ||
|
|
||
| assert_eq!( | ||
| nodename, hostname, | ||
| "uname.nodename and gethostname() must agree", | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn uname_fields_are_non_empty() { | ||
| let mut uts = MaybeUninit::<libc::utsname>::uninit(); | ||
|
|
||
| unsafe { | ||
| assert_ne!(libc::uname(uts.as_mut_ptr()), -1, "uname() failed"); | ||
| let uts = uts.assume_init(); | ||
|
|
||
| let read = |buf: &[libc::c_char], name: &str| -> String { | ||
| CStr::from_ptr(buf.as_ptr()) | ||
| .to_str() | ||
| .unwrap_or_else(|_| panic!("uname.{name} is not valid UTF-8")) | ||
| .to_owned() | ||
| }; | ||
|
|
||
| // Every variable-length field must read as a non-empty string. | ||
| // A leading-zero byte here would mean the field is being read | ||
| // from a misaligned offset (the bug this test guards against). | ||
| assert!(!read(&uts.sysname, "sysname").is_empty()); | ||
| assert!(!read(&uts.nodename, "nodename").is_empty()); | ||
| assert!(!read(&uts.release, "release").is_empty()); | ||
| assert!(!read(&uts.version, "version").is_empty()); | ||
| assert!(!read(&uts.machine, "machine").is_empty()); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Could you drop this test? We don't have concrete testing structure for this type and I'd figure out it before actually adding anything.
View changes since the review