Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/macho/fat_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ def initialize(filename, **opts)

@filename = filename
@options = opts
@raw_data = File.binread(@filename)
File.open(@filename, "rb") do |file|
@raw_data = file.read(Headers::FatHeader.bytesize)
@raw_data ||= ""
populate_fat_header
@raw_data << file.read.to_s
end
populate_fields
end

Expand Down
7 changes: 6 additions & 1 deletion lib/macho/macho_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ def initialize(filename, **opts)

@filename = filename
@options = opts
@raw_data = File.binread(@filename)
File.open(@filename, "rb") do |file|
@raw_data = file.read(Headers::MachHeader.bytesize)
@raw_data ||= ""
populate_mach_header if !opts.fetch(:decompress, false) || !Utils.compressed_magic?(@raw_data.unpack1("N"))
@raw_data << file.read.to_s
end
populate_fields
end

Expand Down
14 changes: 14 additions & 0 deletions test/test_fat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ def test_java_classfile
end
end

def test_invalid_header_is_rejected_before_full_read
tempfile_with_data("invalid_header", [MachO::Headers::FAT_MAGIC, 31].pack("N2") + ("x" * 1024)) do |invalid_header|
assert_raises MachO::JavaClassFileError do
Class.new(MachO::FatFile) do
def populate_fat_header
raise "read entire file" if serialize.bytesize > MachO::Headers::FatHeader.bytesize

super
end
end.new(invalid_header.path)
end
end
end

def test_zero_arch_file
assert_raises MachO::ZeroArchitectureError do
MachO::FatFile.new("test/bin/llvm/macho-invalid-fat-header")
Expand Down
14 changes: 14 additions & 0 deletions test/test_macho.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ def test_truncated_file
end
end

def test_invalid_header_is_rejected_before_full_read
tempfile_with_data("invalid_header", [MachO::Headers::MH_MAGIC, 0, 0, MachO::Headers::MH_EXECUTE, 0, 0, 0].pack("N7") + ("x" * 1024)) do |invalid_header|
assert_raises MachO::CPUTypeError do
Class.new(MachO::MachOFile) do
def populate_mach_header
raise "read entire file" if serialize.bytesize > MachO::Headers::MachHeader.bytesize

super
end
end.new(invalid_header.path)
end
end
end

def test_load_commands
filenames = SINGLE_ARCHES.map { |a| fixture(a, "hello.bin") }

Expand Down
Loading