Repository navigation
Avoid integer overflow when comparing annotation samples - #733
Conversation
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
left a comment
There was a problem hiding this comment.
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.
Add tests for signed and unsigned integer comparisons
Problem
processing.compare_annotationssubtracts 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 theint64range can overflow. Largeuint64offsets 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 atd40be3a13228d72f71b3f7ed50d10e3fb10f32f2byte-matches the tested current-main-plus-fix tree (same files and bytes). This is a merge update, not a history rewrite.bxbutility is not installed, 21 network tests deselected.-W --keep-going -E) passed.An additional 4,000 randomized comparisons on the earlier
98df2c9base 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.0had 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.