Skip to content

Commit c85f9fa

Browse files
committed
internal/span: handle invalid column values to avoid crashing
This might not be necessary after we fix handling for line directives, but it's always better to avoid the panic here. Updates golang/go#34433 Change-Id: Ica4fb571dff6753fb15bf8d397c55f713284aa27 Reviewed-on: https://go-review.googlesource.com/c/tools/+/196662 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
1 parent 59c6680 commit c85f9fa

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

internal/span/utf16.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func ToUTF16Column(p Point, content []byte) (int, error) {
2929
if colZero == 0 {
3030
// 0-based column 0, so it must be chr 1
3131
return 1, nil
32+
} else if colZero < 0 {
33+
return -1, fmt.Errorf("ToUTF16Column: column is invalid (%v)", colZero)
3234
}
3335
// work out the offset at the start of the line using the column
3436
lineOffset := offset - colZero
@@ -41,6 +43,7 @@ func ToUTF16Column(p Point, content []byte) (int, error) {
4143

4244
// Now, truncate down to the supplied column.
4345
start = start[:colZero]
46+
4447
// and count the number of utf16 characters
4548
// in theory we could do this by hand more efficiently...
4649
return len(utf16.Encode([]rune(string(start)))) + 1, nil

0 commit comments

Comments
 (0)