Skip to content

fix: handle nested binary expressions in instrumentFile#25

Merged
fchimpan merged 4 commits into
mainfrom
fix/nested-binary-expr-panic
Mar 24, 2026
Merged

fix: handle nested binary expressions in instrumentFile#25
fchimpan merged 4 commits into
mainfrom
fix/nested-binary-expr-panic

Conversation

@fchimpan

Copy link
Copy Markdown
Owner

Summary

  • Fix panic (slice bounds out of range) when instrumenting nested binary comparisons like (a > b) == flag, (x != nil) == (y != nil), or deeper nesting
  • Rewrite instrumentFile to build replacement text bottom-up: inner mutations are embedded into outer replacement text, so all mutation targets are preserved instead of only the outermost
  • Add comprehensive testdata (testdata/project/nested.go) covering 6 nested comparison patterns that previously triggered the panic

Closes #23

Test plan

  • go test ./... -race passes
  • go vet ./... passes
  • Verified no regression against testdata/project and hashicorp/go-multierror by comparing JSON output (old vs new, identical except duration)
  • Verified old binary panics on reproduction case while new binary instruments all 8 mutation points successfully
  • New testdata/project/nested.go — all 16 nested mutations are killed by tests

Rewrite instrumentFile to use bottom-up replacement building so that
nested binary comparisons (e.g. (a > b) == flag) instrument all
mutation targets instead of panicking on overlapping byte ranges.

Inner replacement text is embedded into outer replacements, and only
root-level replacements are applied to the source.

Closes #23
Add testdata/project/nested.go with patterns that triggered #23:
- comparison in equality: (a > b) == flag
- double nil-check in equality: (x != nil) == (y != nil)
- triple nesting: ((a > b) == flag) != expected
- side-by-side comparisons: (a > b) == (c < d)
- comparison in inequality: (a <= b) != expected
- nil-check with comparison: p == nil, (*p > threshold) == true

Update TestDiscoverAll expected count from 11 to 17.
@fchimpan fchimpan merged commit c93f2d0 into main Mar 24, 2026
1 check passed
@fchimpan fchimpan deleted the fix/nested-binary-expr-panic branch March 25, 2026 07:25
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.

panic: slice bounds out of range when instrumenting nested binary comparisons

1 participant