Skip to content

[COA-2971] Fix Union interval bounds validation to prevent panic - #3

Merged
bjaus merged 2 commits into
mainfrom
COA-2971-fix-union-interval-bounds
Sep 2, 2026
Merged

bjaus merged 2 commits into
mainfrom
COA-2971-fix-union-interval-bounds

Conversation

@bjaus

@bjaus bjaus commented Feb 21, 2026 •

Copy link
Copy Markdown

Summary

  • Prevent interval.Union from returning a computed interval whose start is after its end.
  • Add regression coverage for the real empty-function parsing path, plus existing disjoint and adjacent interval coverage.

Problem

files.GetIntervalsFromFile excludes 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, Union on main can return the inverted intersection. With ignore-empty=true, the CLI passes the result to TotalWhitespace, which slices lines[start:end] and can panic with slice bounds out of range.

Concrete engx manifestation

engx PR #4 added cmd/ktl/internal/auth/audit.go, including this legitimate no-op implementation:

func (noopAuditLogger) Log(context.Context, AuditEvent) {}

The PR diff covered the newly added file, while cov-diff parsed the empty method as an inverted body interval. The old Union could propagate that interval to TotalWhitespace, 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=true can 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. Setting ignore-empty=false avoids this particular slice, but changes the coverage denominator by counting blank lines and still leaves Union able 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 Union now appends a calculated intersection only when start <= 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):

  • Existing preconditions already establish that a valid pair overlaps before the intersection is calculated.
  • For overlapping valid intervals, 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.
  • An independent audit checked all production Union callers, the Action/CLI surface, Docker build, module dependencies, and Action metadata. No inputs, outputs, flags, exported signatures, or dependencies changed.
  • The audit also compared source-faithful old/new models across 58,081 exhaustive valid interval-list pairs and 100,000 randomized valid pairs with no output difference.

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

  • TestUnionIgnoresMalformedBoundsFromEmptyFunction reproduces the parser-generated empty-function path and verifies that no invalid interval reaches TotalWhitespace.
  • The new test fails on parent 02f42376e4ff5b49491c70edaed62d384aa036ed with inverted intervals ({Start:4, End:3}) and passes at this PR head.
  • Local checks passed: go test ./..., go vet ./..., go build ./..., and git diff --check.
  • PR CI is green on the current head: Integration Test, CodeQL, all Wiz scanners, and task-list validation.

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 that Union does not create and propagate inverted intersections from the production parsing path.

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.
@github-actions

Copy link
Copy Markdown

Coverage on new code: 75%

@bjaus

bjaus commented Feb 23, 2026

Copy link
Copy Markdown
Author

Verification

This fix has been tested on rewardStyle/engx#4.

❌ Before fix (using cov-diff@main)

Run: https://github.com/rewardStyle/engx/actions/runs/22156496005

The check-code-coverage job failed with:

panic: runtime error: slice bounds out of range [90:88]

goroutine 1 [running]:
github.com/panagiotisptr/cov-diff/interval.TotalWhitespace(...)
    /cov-diff/interval/interval.go:46 +0xf0

✅ After fix (using cov-diff@COA-2971-fix-union-interval-bounds)

Run: https://github.com/rewardStyle/engx/actions/runs/22311098554

The check-code-coverage job now passes successfully and posts coverage metrics to the PR.

Summary

The fix correctly handles non-overlapping intervals by validating start <= end before appending to the result, preventing the invalid slice bounds that caused the panic.

@bjaus
bjaus marked this pull request as ready for review February 23, 2026 14:58
Copilot AI review requested due to automatic review settings February 23, 2026 14:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 where start > 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.

@bjaus bjaus closed this Sep 2, 2026
@bjaus
bjaus deleted the COA-2971-fix-union-interval-bounds branch September 2, 2026 20:43
@abice
abice restored the COA-2971-fix-union-interval-bounds branch September 2, 2026 22:21
@bjaus bjaus reopened this Sep 2, 2026
@bjaus
bjaus merged commit 5f2b717 into main Sep 2, 2026
9 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.

3 participants