tile_probe: reject a negative or past-the-end --offset with exit 2

This commit is contained in:
xel1uz
2026-11-25 07:06:20 +01:00
parent 42537e7b6f
commit 69e6b59c79
2 changed files with 33 additions and 9 deletions

View File

@ -40,21 +40,37 @@ class TileProbeInputTest(unittest.TestCase):
self.assertIn("missing.bin", p.stderr)
self.assertNotIn("Traceback", p.stderr)
def test_offset_outside_the_file_is_an_error(self):
# Before: -5 quietly probed the last five bytes, the file size and more printed nothing, and both exited 0.
for offset in ("-5", "16384", "99999"):
def test_negative_offset_is_a_usage_error(self):
# Before: -5 quietly probed the file's tail, then exited 1 via a bare SystemExit instead of 2.
p = self.run_tool(self.blob, "--offset", "-5")
self.assertEqual(p.returncode, 2, p.stderr)
self.assertEqual(p.stderr.strip(), "ERROR: --offset must not be negative")
self.assertNotIn("Traceback", p.stderr)
self.assertEqual(p.stdout, "")
def test_offset_at_or_past_the_end_is_a_usage_error(self):
# Before: the file size and beyond printed nothing and exited 1, indistinguishable from a failed probe.
for offset in ("16384", "99999"):
with self.subTest(offset=offset):
p = self.run_tool(self.blob, "--offset", offset)
self.assertEqual(p.returncode, 1, p.stderr)
self.assertIn("outside", p.stderr)
self.assertEqual(p.returncode, 2, p.stderr)
self.assertIn(f"--offset {offset} is past the end of blob", p.stderr)
self.assertIn("(16384 bytes)", p.stderr)
self.assertNotIn("Traceback", p.stderr)
self.assertEqual(p.stdout, "")
def test_empty_file_is_an_error(self):
def test_empty_file_is_a_short_sample_not_a_bad_offset(self):
empty = os.path.join(self.tmp.name, "empty.bin")
open(empty, "wb").close()
p = self.run_tool(empty)
self.assertEqual(p.returncode, 1, p.stderr)
self.assertIn("0 bytes", p.stderr)
self.assertIn("no layout fits", p.stderr)
self.assertNotIn("past the end", p.stderr)
# offset 0 on an empty blob is fine, offset 5 is not: that one is past the end, exit 2
p = self.run_tool(empty, "--offset", "5")
self.assertEqual(p.returncode, 2, p.stderr)
self.assertIn("is past the end of blob", p.stderr)
self.assertIn("(0 bytes)", p.stderr)
def test_a_sample_with_no_candidate_layout_is_an_error(self):
# Before: an 8-byte or 4 KiB blob printed nothing, wrote no preview and exited 0.

View File

@ -178,6 +178,16 @@ def main():
if not os.path.exists(a.blob):
print(f"ERROR: {a.blob}: does not exist", file=sys.stderr)
return 2
# An --offset with no bytes left to probe is a usage error, not a failed probe: exit 2 like the other argument
# checks (it used to reach the probe and exit 1 as "no layout fits"). An empty blob at offset 0 is the
# exception: the offset is fine there, the sample itself is just too short, so that stays on the result path.
size = os.path.getsize(a.blob) if not os.path.isdir(a.blob) else 0
if a.offset < 0:
print("ERROR: --offset must not be negative", file=sys.stderr)
return 2
if a.offset >= size and (size or a.offset):
print(f"ERROR: --offset {a.offset} is past the end of blob {a.blob} ({size} bytes)", file=sys.stderr)
return 2
if a.out:
check_preview_path(a.out, a.blob)
if np is None:
@ -188,8 +198,6 @@ def main():
data = f.read()
except OSError as e:
raise SystemExit(f"{a.blob}: {e.strerror or e}")
if not 0 <= a.offset < len(data): # a negative offset used to probe the file's tail, one past the end nothing
raise SystemExit(f"--offset {a.offset} is outside {a.blob} ({len(data)} bytes)")
res = probe(data, a.offset)
if not res:
# Every candidate needs at least 16 rows of blocks at one of the widths; a short sample used to print