Skip to content

BUILD: normalize negative indices in zero_span range helpers - #4396

Open
ray3242 wants to merge 2 commits into
ItsNotRexed:mainfrom
ray3242:build/zero-span-negative-indices
Open

ray3242 wants to merge 2 commits into
ItsNotRexed:mainfrom
ray3242:build/zero-span-negative-indices

Conversation

@ray3242

@ray3242 ray3242 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as a new BUILD item: #4257 (comment).

What and why

tools/zero_span.py documents that its counts match len(buf[start:end]) and that all_zero is true only for an empty or all-zero slice. It clipped end to len(buf) but never normalized negative indices the way Python slicing does, so those two guarantees broke for negative ranges:

  • gap_split(b'ABCDEFGHIJ'*20, -5, 10) returned (0, 15) for buf[-5:10], which is empty.
  • gap_split(buf, -30, 200) returned 230 bytes for a 30-byte slice.
  • all_zero(buf, 0, -5) returned True for 195 non-zero bytes.

Both helpers now normalize both endpoints with slice(start, end).indices(len(buf)), which implements Python's own slice rules (negative indices, clamping and empty ranges). gap_split keeps the old start > end → 0 bytes behavior via max(0, end - start). Non-negative behavior is byte-for-byte unchanged.

Scope: this is a correctness fix for the public helper; current in-tree callers pass non-negative stream offsets, so it is not a live scanner failure.

Tested

AI-assisted (OpenCode). Windows 11, Python 3.13.12, CPU only, no GPU and no game data.

  • New tests/test_zero_span_negative.py (4 tests, parameterized over nine ranges and zero/non-zero buffers): 14 assertion failures on current main (03b09a08) and all pass on this branch.
  • Existing python -I tests/test_zero_span.py: 3 OK in 9.3 s on this branch (no regression, including the 48 MiB gap and all nine scanners).
  • python -m compileall -q tools tests: clean.

No file overlap with open PR #4021: that PR touches tests/test_zero_span.py, while this one changes tools/zero_span.py and adds a separate test file.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. Synthetic buffers only.
  • No new translator rule (helper correctness only).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this at 17e42cc4 on Windows 11: test_zero_span_negative.py 4 OK, test_zero_span.py 3 OK; git diff --check clean.

Negative indices in the zero_span range helpers are normalized, and the existing range tests pass. No blocking issue.

@molodayamama

Copy link
Copy Markdown

Independent check of 17e42cc470d83edebc64ad0b6a27f7611e546996 on Windows 10 22H2 / CPython 3.12.12. Both test_zero_span_negative.py (4/4) and test_zero_span.py (3/3) passed.

I also compared all_zero and gap_split to Python slicing over 95,631 synthetic cases: bytes, bytearray, memoryview, endpoints from below -len to above len, and endpoints at +/-2**65. That includes six cases on a real Windows mmap with a nonzero byte exactly at the 4 MiB window boundary, checking that negative endpoints on either side include or exclude it correctly. All matched the reference. No game files. AI-assisted (Codex).

@tupacfan04

Copy link
Copy Markdown
Contributor

PASS. slice(...).indices(len(buf)) at tools/zero_span.py:16,30 implements Python slice rules, max(0, end - start) keeps the empty range at (0, 0), and the spot checks reproduce the body (gap_split(DATA, -5, 10) == (0, 0), (-30, 200) == (0, 30), all_zero(DATA, 0, -5) is False). Reverting to HEAD~1 gives exactly 14 failures; the existing 3 tests and the positive-range test pass; compileall and merge are clean.

Minor: "wrap like the ISA" is really slice semantics, and it is only correct because both callers address through slices (tests/test_zero_span_negative.py:26-52) - worth saying that in the docstring.

Approve. AI-assisted review, no game files, no GPU.

@ray3242

ray3242 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@tupacfan04 — thanks, and I've taken the minor point: tools/zero_span.py's docstring now states explicitly that the range is normalized with Python's slice rules and that this is the right contract because callers address the buffer through slices. Pushed as 16ec15d4 to the PR branch; both test files still pass (4/4 and 3/3) and git diff --check is clean. AI-assisted (OpenCode).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants