Skip to content

Mach-O: Parse macOS version#525

Open
CamJN wants to merge 7 commits into
m4b:masterfrom
CamJN:master
Open

Mach-O: Parse macOS version#525
CamJN wants to merge 7 commits into
m4b:masterfrom
CamJN:master

Conversation

@CamJN

@CamJN CamJN commented Apr 7, 2026

Copy link
Copy Markdown

Possible implementation for #514

@m4b m4b left a comment

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.

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 Outdated
Comment thread src/mach/version.rs Outdated
Comment thread src/mach/version.rs Outdated
Comment thread src/mach/version.rs
Comment thread src/mach/version.rs Outdated
@CamJN CamJN requested a review from m4b April 21, 2026 17:43

@m4b m4b left a comment

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.

almost there, just need to address some of the issues I flagged, thank you!

Comment thread src/mach/version.rs
Comment on lines +19 to +22
pub struct Version {
pub major: u32,
pub minor: u32,
pub patch: u32,

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.

please document all of this, public things, especially new ones, should be doc'd

Comment thread src/mach/version.rs
use crate::mach::load_command::CommandVariant;

if_std! {
use crate::error;

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.

iirc, error is not under std; and I don't think cmp or str or fmt is either, they should be under core?

Comment thread src/mach/version.rs
// X.Y.Z is encoded in nibbles xxxx.yy.zz
// 12.6 = 0b0000_0000_0000_1100_0000_0110_0000_0000
Self {
major: (packed & 0b1111_1111_1111_1111_0000_0000_0000_0000u32) >> 16,

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.

nit: i think we use hex for other bitmasks in other parts of codebase, might want to be uniform with that

Comment thread src/mach/version.rs
}

impl PartialEq for Version {
fn eq(&self, other: &Self) -> bool {

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.

you should be able to just #[derive(PartialEq)

Comment thread src/mach/version.rs
Comment on lines +68 to +87
impl Ord for Version {
fn cmp(&self, other: &Self) -> Ordering {
let mao = self.major.cmp(&other.major);
let mio = self.minor.cmp(&other.minor);
let pao = self.patch.cmp(&other.patch);
if mao == Ordering::Equal && mio == Ordering::Equal {
pao
} else if mao == Ordering::Equal {
mio
} else {
mao
}
}
}

impl PartialOrd for Version {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

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, you should be able to derive this (and it also doesn't require std)

Comment thread src/mach/version.rs
.split('.')
.map(|p| p.parse::<u32>().unwrap_or(0))
.take(3)
.collect::<VecDeque<u32>>();

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 allocates a VecDequeu here just to pop the front; i think you can rewrite this to not allocate, and please do that. thank you for updating with fallible methods though.

Comment thread src/mach/version.rs
}

impl Mach<'_> {
pub fn versions(self) -> HashMap<CpuType, Version> {

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.

needs docstring

In general, I don't necessarily like a method hanging off of Mach here in this file, as it's not local anymore to Mach. This also allocates a hashmap; I think it would be better to probably return an impl Iterator<(CpuType, Version)>; this way users can allocate if they want, etc.

Comment thread src/mach/version.rs
use crate::mach::MachO;
use crate::mach::load_command::CommandVariant;

if_std! {

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.

i think you can remove this entire block once you get rid of the allocations

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