diff --git a/internal/span/utf16.go b/internal/span/utf16.go index 0d356e2b..4439acb6 100644 --- a/internal/span/utf16.go +++ b/internal/span/utf16.go @@ -35,9 +35,14 @@ func ToUTF16Column(p Point, content []byte) (int, error) { if lineOffset < 0 || offset > len(content) { return -1, fmt.Errorf("ToUTF16Column: offsets %v-%v outside file contents (%v)", lineOffset, offset, len(content)) } - // use the offset to pick out the line start + // Use the offset to pick out the line start. + // This cannot panic: offset > len(content) and lineOffset < offset. start := content[lineOffset:] - // now truncate down to the supplied column + + // Now, truncate down to the supplied column. + if col >= len(start) { + return -1, fmt.Errorf("ToUTF16Column: line (%v) is shorter than column (%v)", len(start), col) + } start = start[:col] // and count the number of utf16 characters // in theory we could do this by hand more efficiently...