From 69e6b59c7941dc1180f8f4a705b1a311d7d61d5e Mon Sep 17 00:00:00 2001 From: xel1uz <16402913+xel1uz@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:06:20 +0100 Subject: [PATCH] tile_probe: reject a negative or past-the-end --offset with exit 2 --- tests/test_tile_probe.py | 30 +++++++++++++++++++++++------- tools/tile_probe.py | 12 ++++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/tests/test_tile_probe.py b/tests/test_tile_probe.py index 63af7251d..ef40bb5db 100644 --- a/tests/test_tile_probe.py +++ b/tests/test_tile_probe.py @@ -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. diff --git a/tools/tile_probe.py b/tools/tile_probe.py index 73b8c811c..107d3d2c4 100644 --- a/tools/tile_probe.py +++ b/tools/tile_probe.py @@ -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