Skip to content

Commit 0087a97

Browse files
committed
BUG: fix macho detection for tiny files.
macholib assumes any path given to MachO constructor is at least 4 bytes.
1 parent cf18c98 commit 0087a97

4 files changed

Lines changed: 19 additions & 1 deletion

File tree

machotools/detect.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import os
2+
13
from macholib.MachO import MachO
24

35
def detect_macho_type(path):
46
"""
57
Returns None if not a mach-o.
8+
9+
Raise an error for non-existing paths.
610
"""
11+
if os.stat(path).st_size < 4:
12+
return None
13+
714
try:
815
p = MachO(path)
916
except ValueError as e:
@@ -18,6 +25,9 @@ def detect_macho_type(path):
1825

1926
def is_macho(path):
2027
"""Return True if the given path is a Mach-O binary."""
28+
if os.stat(path).st_size < 4:
29+
return None
30+
2131
try:
2232
MachO(path)
2333
return True

machotools/tests/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
NO_MACHO_FILE = op.join(DYLIB_DIRECTORY, "Makefile")
3131

32+
TINY_FILE = op.join(DYLIB_DIRECTORY, "tiny")
33+
3234
@contextlib.contextmanager
3335
def mkdtemp():
3436
d = tempfile.mkdtemp()

machotools/tests/data/tiny

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a

machotools/tests/test_detect.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
from machotools.detect import is_macho, is_dylib, is_executable, is_bundle
44

5-
from machotools.tests.common import FOO_DYLIB, SIMPLE_MAIN, SIMPLE_BUNDLE, NO_MACHO_FILE
5+
from machotools.tests.common import (
6+
FOO_DYLIB, SIMPLE_MAIN, SIMPLE_BUNDLE, NO_MACHO_FILE, TINY_FILE
7+
)
68

79
class TestDetect(unittest.TestCase):
10+
def test_tiny_file(self):
11+
self.assertFalse(is_macho(TINY_FILE))
12+
813
def test_dylib(self):
914
self.assertTrue(is_dylib(FOO_DYLIB))
1015
self.assertFalse(is_bundle(FOO_DYLIB))

0 commit comments

Comments
 (0)