Skip to content

Mach-O: Parse macOS version#525

Open
CamJN wants to merge 5 commits intom4b:masterfrom
CamJN:master
Open

Mach-O: Parse macOS version#525
CamJN wants to merge 5 commits intom4b:masterfrom
CamJN:master

Conversation

@CamJN
Copy link
Copy Markdown

@CamJN CamJN commented Apr 7, 2026

Possible implementation for #514

Copy link
Copy Markdown
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unwraps need to be addressed, before can consider to merge. Additionally, it may be more idiomatic to have this be a version method on a MachO, and returns an Optional<Version>; I don't think we have anywhere else where we have a From method on MachO that returns some type, so it might be a little undiomatic in this crate to do that.

Comment thread src/mach/version.rs
Mach::Fat(f) => {
match f
.find(|r| {
r.unwrap().cputype
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this unwrap panic (i.e., can a malformed or whatever binary cause this unwrap to panic?); if not please add documentation explaining why not

Comment thread src/mach/version.rs
== match env::var("CARGO_CFG_TARGET_ARCH").as_deref() {
Ok("x86_64") => CPU_TYPE_X86_64,
Ok("aarch64") => CPU_TYPE_ARM64,
_ => panic!("unknown arch"),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this definitely seems like it can panic

Comment thread src/mach/version.rs
}

impl From<Mach<'_>> for Version {
fn from(b: Mach) -> Self {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so there are multiple unwraps in this function, which indicates to me the choice of From is not correct; TryFrom is probably a better choice, or if you need a parsing context, it would be more idiomatic in this crate to use scroll's TryFromCtx

Comment thread src/mach/version.rs
impl FromStr for Version {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all these unwraps need to be map_err'd or documented why they cannot fail

Comment thread src/mach/version.rs
CommandVariant::BuildVersion(v) => Some(v.minos),
_ => None,
})
.unwrap();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, unwrap again, needs documentation why it is infallible, or choice TryFrom/TryFromCtx, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants