Skip to content

Commit b309bd4

Browse files
authored
Fix magic number printing (#53)
1 parent 1c5a0fe commit b309bd4

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

src/COFF/COFFHandle.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ function readmeta(io::IO, ::Type{H}) where {H <: COFFHandle}
4949
magic = [read(io, UInt8) for idx in 1:4]
5050
if any(magic .!= PE_magic)
5151
msg = """
52-
Magic Number 0x$(join(string.(magic, base=16),"")) does not match expected PE
53-
magic number 0x$(join("", string.(PE_magic, base=16)))
52+
Magic Number 0x$(join(string.(magic, base=16, pad=2),"")) does not match expected PE
53+
magic number 0x$(join(string.(PE_magic, base=16, pad=2),""))
5454
"""
5555
throw(MagicMismatch(replace(strip(msg), "\n" => " ")))
5656
end

src/ELF/ELFHandle.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ function readmeta(io::IO, ::Type{H}) where {H <: ELFHandle}
3838
magic = [read(io, UInt8) for idx in 1:4]
3939
if any(magic .!= elven_magic)
4040
msg = """
41-
Magic Number 0x$(join(string.(magic, base=16),"")) does not match expected ELF
42-
magic number 0x$(join("", string.(elven_magic, base=16)))
41+
Magic Number 0x$(join(string.(magic, base=16, pad=2),"")) does not match expected ELF
42+
magic number 0x$(join(string.(elven_magic, base=16, pad=2),""))
4343
"""
4444
throw(MagicMismatch(replace(strip(msg), "\n" => " ")))
4545
end

src/MachO/MachOHeader.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function macho_header_type(magic::UInt32)
4747
elseif magic in (METALLIB_MAGIC,)
4848
return MetallibHeader{MachOHandle}
4949
else
50-
throw(MagicMismatch("Invalid Magic ($(string(magic, base=16)))!"))
50+
throw(MagicMismatch("Invalid Magic (0x$(string(magic, base=16, pad=8)))!"))
5151
end
5252
end
5353

@@ -63,7 +63,7 @@ function macho_is64bit(magic::UInt32)
6363
elseif magic in (MH_MAGIC, MH_CIGAM, FAT_MAGIC, FAT_CIGAM, FAT_MAGIC_METAL, FAT_CIGAM_METAL, METALLIB_MAGIC)
6464
return false
6565
else
66-
throw(MagicMismatch("Invalid Magic ($(string(magic, base=16)))!"))
66+
throw(MagicMismatch("Invalid Magic (0x$(string(magic, base=16, pad=8)))!"))
6767
end
6868
end
6969

@@ -79,7 +79,7 @@ function macho_endianness(magic::UInt32)
7979
elseif magic in (MH_MAGIC, MH_MAGIC_64, FAT_MAGIC, FAT_MAGIC_METAL, METALLIB_MAGIC)
8080
return :LittleEndian
8181
else
82-
throw(MagicMismatch("Invalid Magic ($(string(magic, base=16)))!"))
82+
throw(MagicMismatch("Invalid Magic (0x$(string(magic, base=16, pad=8)))!"))
8383
end
8484
end
8585

test/runtests.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,27 @@ using Test
1212
end
1313
end
1414

15+
function check_magic_mismatch(path, HandleType)
16+
err = try
17+
readmeta(open(path, "r"), HandleType)
18+
catch err
19+
err
20+
end
21+
22+
@test err isa MagicMismatch
23+
if HandleType === ELFHandle
24+
@test occursin(r"Magic Number 0x[a-f0-9]{8} does not match expected ELF magic number 0x7f454c46", repr(err))
25+
elseif HandleType === COFFHandle
26+
@test occursin(r"Magic Number 0x[a-f0-9]{8} does not match expected PE magic number 0x50450000", repr(err))
27+
elseif HandleType === MachOHandle
28+
@test occursin(r"Invalid Magic \(0x[a-f0-9]{8}\)\!", repr(err))
29+
else
30+
@assert false "unexpected handle type"
31+
end
32+
33+
return nothing
34+
end
35+
1536
function test_libfoo_and_fooifier(fooifier_path, libfoo_path)
1637
# Actually read it in
1738
oh_exe = only(readmeta(open(fooifier_path, "r")))
@@ -43,6 +64,12 @@ function test_libfoo_and_fooifier(fooifier_path, libfoo_path)
4364
# Test that we got the right type
4465
@test typeof(oh) <: H
4566

67+
# Test that the wrong types all error as expected
68+
for MismatchedType in (ELFHandle, COFFHandle, MachOHandle)
69+
H === MismatchedType && continue
70+
check_magic_mismatch(fooifier_path, MismatchedType)
71+
end
72+
4673
# Test that we got the right number of bits
4774
@test is64bit(oh) == (bits == "64")
4875
@test platforms_match(Platform(oh), platforms[dir_path])

0 commit comments

Comments
 (0)