From f9398171876ba36b8a4d8264091eaf07c1ed776e Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 18 Apr 2019 16:55:24 -0400 Subject: [PATCH] internal/lsp: add an additional check to ToUTF16Column This change will at least stop the panic from occurring. Updates golang/go#31341 Change-Id: I6b06941cfb6c020d2b37813573cb0dc068d54e65 Reviewed-on: https://go-review.googlesource.com/c/tools/+/172667 Run-TryBot: Rebecca Stambler Reviewed-by: Ian Cottrell --- internal/span/utf16.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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...