Skip to content

Validate Mach-O headers before full reads#998

Open
MikeMcQuaid wants to merge 1 commit into
mainfrom
incremental-macho-reads
Open

Validate Mach-O headers before full reads#998
MikeMcQuaid wants to merge 1 commit into
mainfrom
incremental-macho-reads

Conversation

@MikeMcQuaid

Copy link
Copy Markdown
Member
  • Avoid loading large invalid files before MachOFile and FatFile can reject malformed headers.
  • Keep successful parsing on one handle so serialisation and mutation continue to use complete file data.
  • Preserve compressed Mach-O handling when decompression needs the full payload.

Fixes #23

Copilot AI review requested due to automatic review settings July 10, 2026 16:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates MachO::MachOFile and MachO::FatFile initialization to validate headers using an incremental read (header first, then the remainder only if the header is acceptable), reducing unnecessary memory use when rejecting malformed binaries while preserving the “single handle” parsing flow and compressed Mach-O behavior.

Changes:

  • Read only the header initially in MachOFile/FatFile, validating it before reading the remainder of the file.
  • Add regression tests ensuring invalid headers are rejected before a full file read occurs.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
lib/macho/macho_file.rb Switches from File.binread to incremental header-first read with early header validation.
lib/macho/fat_file.rb Switches from File.binread to incremental header-first read with early fat header validation.
test/test_macho.rb Adds a test to ensure invalid Mach-O headers fail before full-file reads.
test/test_fat.rb Adds a test to ensure invalid fat headers fail before full-file reads.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/macho/macho_file.rb Outdated
Comment thread lib/macho/fat_file.rb Outdated
- Avoid loading large invalid files before `MachOFile` and `FatFile`
  can reject malformed headers.
- Keep successful parsing on one handle so serialisation and mutation
  continue to use complete file data.
- Preserve compressed Mach-O handling when decompression needs the full
  payload.
- Coerce tail reads so a `nil` EOF cannot replace parser errors with
  `TypeError`.
@MikeMcQuaid MikeMcQuaid force-pushed the incremental-macho-reads branch from be4666a to b571e44 Compare July 10, 2026 18:20
@MikeMcQuaid MikeMcQuaid enabled auto-merge July 10, 2026 18:54
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.

Incremental reads for MachOFile and FatFile

2 participants