Skip to content

Commit daae490

Browse files
committed
fix(lint): clamp negative offsets in ColumnOfOffset
The binary-search rewrite only clamped the upper bound (offset > len(Source)); a negative offset now echoed back out unclamped (ColumnOfOffset(-5) == -4) instead of the pre-refactor behavior of always returning 1 for any negative offset. No shipped rule call site can currently trigger this (all pass an already-guarded, non-negative value), but the contract should still match LineOfOffset's and the prior implementation's handling of out-of-domain input. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UCh2j2oeSw6UgNsmpjRonL
1 parent 1aba2d9 commit daae490

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

internal/lint/file.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,9 @@ func (f *File) ColumnOfOffset(offset int) int {
554554
if offset > len(f.Source) {
555555
offset = len(f.Source)
556556
}
557+
if offset < 0 {
558+
offset = 0
559+
}
557560
// Reuses LineOfOffset's cached newline index via the same binary
558561
// search instead of scanning backward from offset byte by byte —
559562
// O(log n) in the newline count instead of O(line length). A single

internal/lint/lint_coverage_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ func TestColumnOfOffset_PastEOFClamps(t *testing.T) {
280280
assert.Equal(t, 4, f.ColumnOfOffset(999))
281281
}
282282

283+
func TestColumnOfOffset_NegativeOffsetClamps(t *testing.T) {
284+
// A negative offset clamps to the start of the file (column 1),
285+
// mirroring the upper-bound EOF clamp above.
286+
f := &File{Source: []byte("line1\nline2\n")}
287+
assert.Equal(t, 1, f.ColumnOfOffset(-1))
288+
assert.Equal(t, 1, f.ColumnOfOffset(-100))
289+
}
290+
283291
func TestColumnOfOffset_AtNewline(t *testing.T) {
284292
// The newline itself sits at the end of its line.
285293
f := &File{Source: []byte("ab\nc")}

0 commit comments

Comments
 (0)