Repository navigation
[COA-2971] Fix Union interval bounds validation to prevent panic - #3
Conversation
The Union function computes intersection intervals by taking max(start) and min(end) of two intervals. However, when intervals don't actually overlap (e.g., [90,95] and [80,88]), this produces invalid intervals where start > end (e.g., [90,88]). This causes a panic in TotalWhitespace when it tries to slice with invalid bounds: `lines[90:88]` -> "slice bounds out of range [90:88]" The fix validates that start <= end before appending to the result, which correctly handles non-overlapping intervals by excluding them. Added test cases for non-overlapping and adjacent intervals.
|
Coverage on new code: 75% |
VerificationThis fix has been tested on rewardStyle/engx#4. ❌ Before fix (using
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a panic in the interval.Union() function that occurred when processing non-overlapping intervals. The function computes interval intersections using max(a.Start, b.Start) for the start and min(a.End, b.End) for the end. When intervals don't actually overlap, this can produce invalid intervals where start > end, which causes panics in downstream functions like TotalWhitespace().
The fix adds defensive validation to check start <= end before appending intervals, and includes test cases for non-overlapping and adjacent interval scenarios that previously caused panics.
Changes:
- Added bounds validation in
interval.Union()to prevent creating invalid intervals wherestart > end - Added test case for non-overlapping intervals
- Added test case for adjacent but non-overlapping intervals
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| interval/interval.go | Added defensive check to validate start <= end before appending intervals in Union function |
| interval/interval_test.go | Added two test cases to verify non-overlapping intervals produce empty results |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
interval.Unionfrom returning a computed interval whose start is after its end.Problem
files.GetIntervalsFromFileexcludes a function's braces when it determines the lines that should count toward coverage. For an empty function, that can produce inverted bounds (for example,{Start: 4, End: 3}).When a changed-lines interval overlaps that malformed value,
Uniononmaincan return the inverted intersection. Withignore-empty=true, the CLI passes the result toTotalWhitespace, which sliceslines[start:end]and can panic withslice bounds out of range.Concrete engx manifestation
engx PR #4 added
cmd/ktl/internal/auth/audit.go, including this legitimate no-op implementation:The PR diff covered the newly added file, while cov-diff parsed the empty method as an inverted body interval. The old
Unioncould propagate that interval toTotalWhitespace, causing the bounds panic. The historical job log and artifact have expired, but the PR source shape, workflow inputs, parser behavior, and old interval logic establish this deterministic path.This is not an engx-specific defect: any cov-diff Action or CLI consumer that analyzes an empty/tiny changed Go function with
ignore-empty=truecan reach the same path.Why the fix belongs in cov-diff
Filtering engx's coverage profile cannot prevent this failure because cov-diff parses changed source and calculates whitespace before it consults coverage blocks. Filtering
/cmd/from the diff would hide legitimate CLI changes from new-line coverage. Settingignore-empty=falseavoids this particular slice, but changes the coverage denominator by counting blank lines and still leavesUnionable to emit invalid intervals. Refactoring engx's empty methods would be a brittle workaround that does not protect future code or other consumers.The shared tool must not create and propagate an impossible interval. This PR corrects that behavior at the shared boundary.
Change
The guard in
Unionnow appends a calculated intersection only whenstart <= end. Invalid, computed intersections are discarded before they can reach whitespace or coverage processing.Backward-compatibility analysis
This is backward compatible for all well-formed intervals (
Start <= End):max(a.Start, b.Start) <= min(a.End, b.End), so the new guard is always true. The same interval is appended, the same indices advance, and the unchanged final join runs.Unioncallers, the Action/CLI surface, Docker build, module dependencies, and Action metadata. No inputs, outputs, flags, exported signatures, or dependencies changed.For malformed input, behavior intentionally changes: cov-diff no longer emits an inverted intersection that can panic later. This is defensive behavior, not an API change.
Regression and validation evidence
TestUnionIgnoresMalformedBoundsFromEmptyFunctionreproduces the parser-generated empty-function path and verifies that no invalid interval reachesTotalWhitespace.02f42376e4ff5b49491c70edaed62d384aa036edwith inverted intervals ({Start:4, End:3}) and passes at this PR head.go test ./...,go vet ./...,go build ./..., andgit diff --check.Scope and residual limitations
This change does not alter how empty functions are parsed and does not add a general bounds check to
TotalWhitespace. Callers that directly provide arbitrary out-of-range intervals remain outside this narrow fix. The patch specifically ensures thatUniondoes not create and propagate inverted intersections from the production parsing path.