Skip to content

Commit 660737b

Browse files
committed
Fix magic number printing
1 parent 427b624 commit 660737b

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,37 @@ function test_libfoo_and_fooifier(fooifier_path, libfoo_path)
4343
# Test that we got the right type
4444
@test typeof(oh) <: H
4545

46+
# Test that the wrong types all error as expected
47+
if H !== ELFHandle
48+
err = try
49+
readmeta(open(fooifier_path, "r"), ELFHandle)
50+
catch err
51+
err
52+
end
53+
@test err isa MagicMismatch
54+
@test occursin(r"Magic Number 0x[a-f0-9]{8} does not match expected ELF magic number 0x7f454c46", repr(err))
55+
end
56+
57+
if H !== COFFHandle
58+
err = try
59+
readmeta(open(fooifier_path, "r"), COFFHandle)
60+
catch err
61+
err
62+
end
63+
@test err isa MagicMismatch
64+
@test occursin(r"Magic Number 0x[a-f0-9]{8} does not match expected PE magic number 0x50450000", repr(err))
65+
end
66+
67+
if H !== MachOHandle
68+
err = try
69+
readmeta(open(fooifier_path, "r"), MachOHandle)
70+
catch err
71+
err
72+
end
73+
@test err isa MagicMismatch
74+
@test occursin(r"Invalid Magic \(0x[a-f0-9]{8}\)\!", repr(err))
75+
end
76+
4677
# Test that we got the right number of bits
4778
@test is64bit(oh) == (bits == "64")
4879
@test platforms_match(Platform(oh), platforms[dir_path])

0 commit comments

Comments
 (0)