Skip to content

Avoid integer overflow when comparing annotation samples - #733

Merged
tompollard merged 6 commits into
MIT-LCP:mainfrom
Kulraj69:fix-comparison-integer-differences
Nov 25, 2026
Merged

tompollard merged 6 commits into
MIT-LCP:mainfrom
Kulraj69:fix-comparison-integer-differences

Conversation

@Kulraj69

@Kulraj69 Kulraj69 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

processing.compare_annotations subtracts NumPy sample-position scalars before taking their absolute difference. Unsigned subtraction can wrap when a nearby detection follows the reference, while a signed difference spanning the int64 range can overflow. Large uint64 offsets can also lose the intended small difference.

This affects the public comparison API. For unsigned reference positions [100, 200] and detections [101, 201], a five-sample window should match both pairs.

Change

Convert NumPy integer scalars to Python integers before subtracting, at all three sample-difference sites in Comparitor. After review, sorted-order validation also compares adjacent values directly instead of subtracting, and validates each input independently. Floating-point inputs are not truncated. The matching order and tie-breaking logic are unchanged.

Add eighteen regressions covering unsigned types, large offsets, early/late detections, an extreme signed span, a fractional-distance control and input immutability. Add a changelog fragment.

This is separate from #606's one-to-one matching correction and #566's empty-input statistics handling.

Validation

Python 3.12.16. The branch now merges upstream main 192465c645f02fb3b94c0989bef82fa1d74fc576, including #734's dependency exclusion. The complete downloaded source tree at d40be3a13228d72f71b3f7ed50d10e3fb10f32f2 byte-matches the tested current-main-plus-fix tree (same files and bytes). This is a merge update, not a history rewrite.

  • Original ten-case file against current upstream without this PR: 7 failed, 3 passed.
  • Expanded eighteen-case file against the first published revision: 6 failed, 12 passed, including two-element signed-extrema and descending-unsigned constructor cases.
  • Regression file plus processing and HR tests on current upstream with the patch: 234 passed.
  • Full available offline suite on that tree: 848 passed, 4 skipped because the external WFDB bxb utility is not installed, 21 network tests deselected.
  • Pinned Ruff 0.17.3 check and format passed for the changed Python files.
  • Strict Sphinx 9.0.4 HTML build (-W --keep-going -E) passed.

An additional 4,000 randomized comparisons on the earlier 98df2c9 base preserved the matching results after translating small sorted inputs into unsigned and large signed integer ranges. This is a differential check, not proof that the matching algorithm is globally optimal.

Other private fixes were not applied. The network suite, other Python/dependency versions and remote CI were not run for this change. Prepared and tested with AI assistance; no independent human review is claimed.

CI note after review: the pre-refresh final-head workflow failed (13 jobs failed, four passed). The inspected Ubuntu Python 3.12 log stopped before tests because fonttools==4.69.0 had no compatible distribution. Upstream #734 excludes that incomplete release; this branch now includes that upstream fix. This establishes the cause for the inspected job only. Fresh local offline validation on the refreshed tree passed 848 tests, with the same four skips and 21 network exclusions. New-head CI and maintainer re-review remain pending.

Added a helper function to calculate sample differences without truncating float inputs. Updated references to use the new function for improved clarity.
Add tests for integer and fractional annotation comparisons, including overflow handling.
Updated documentation to highlight integer overflow prevention in annotation comparison.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The matching calculations now widen NumPy integer samples correctly, and the focused tests pass. I found one remaining overflow in the same comparison path: Comparitor.__init__ still uses np.diff to validate sorted positions before matching.

For a valid sorted int64 array [-2**63, 2**63 - 1], np.diff wraps to -1, so compare_annotations raises ValueError. Conversely, a descending uint64 array [200, 100] wraps to a large positive difference and is accepted. I reproduced both on this revision. Please compare adjacent positions directly, without subtraction, and add two-element regression cases for these inputs. The current signed-span test uses singletons and cannot exercise the constructor check.

The 226 focused tests and Ruff checks passed locally. The failing CI matrix appears to stop during dependency installation because fonttools==4.69.0 has no compatible distribution on those runners; that is separate from this code finding.

@tompollard

Copy link
Copy Markdown
Member

@Kulraj69 thanks for the fix! Rebasing on main should resolve the failing tests (since #734 was merged).

@tompollard
tompollard merged commit 3bef000 into MIT-LCP:main Nov 25, 2026
19 checks passed
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.

2 participants