Validate Mach-O headers before full reads#998
Open
MikeMcQuaid wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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.
- 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`.
be4666a to
b571e44
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
MachOFileandFatFilecan reject malformed headers.Fixes #23