From c3dc7cca1094cbeea65922581c4284c65b5d7e3e Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 3 Apr 2024 19:59:45 -0500 Subject: [PATCH] Fixed underflow issue with truncating test/bench -C/--context There was no check on context > stdout, so requesting more context than was actually printed by the test could result in a negative value. Python "helpfully" interpreted this as a negative index, resulting in somewhat random context lengths. This, combined with my tendency to just default to a large number like --context=100, led to me thinking a test was printing much less than it actually was... Don't get me wrong, I love Python, and I think Python's negative indices are a clever way to add flexibility to slice notation, but the value-dependent semantics are a pretty unfortunate footgun... --- scripts/bench.py | 2 +- scripts/test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/bench.py b/scripts/bench.py index 8d12af22..ad277ac3 100755 --- a/scripts/bench.py +++ b/scripts/bench.py @@ -1334,7 +1334,7 @@ def run(runner, bench_ids=[], **args): stdout = failure.stdout if failure.assert_ is not None: stdout = stdout[:-1] - for line in stdout[len(stdout)-args.get('context', 5):]: + for line in stdout[max(len(stdout)-args.get('context', 5), 0):]: sys.stdout.write(line) if failure.assert_ is not None: diff --git a/scripts/test.py b/scripts/test.py index 7acadb29..eaa56598 100755 --- a/scripts/test.py +++ b/scripts/test.py @@ -1306,7 +1306,7 @@ def run(runner, test_ids=[], **args): stdout = failure.stdout if failure.assert_ is not None: stdout = stdout[:-1] - for line in stdout[len(stdout)-args.get('context', 5):]: + for line in stdout[max(len(stdout)-args.get('context', 5), 0):]: sys.stdout.write(line) if failure.assert_ is not None: