Skip to content

Fix panic in LSP formatting with multi-byte characters and trailing newlines #1391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/ls/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,17 @@ func (c *Converters) PositionToLineAndCharacter(script Script, position core.Tex

start := lineMap.LineStarts[line]

// Ensure position doesn't exceed text length to avoid slice bounds errors
text := script.Text()
textLen := core.TextPos(len(text))
position = min(position, textLen)

var character core.TextPos
if lineMap.AsciiOnly || c.positionEncoding == lsproto.PositionEncodingKindUTF8 {
character = position - start
} else {
// We need to rescan the text as UTF-16 to find the character offset.
for _, r := range script.Text()[start:position] {
for _, r := range text[start:position] {
character += core.TextPos(utf16.RuneLen(r))
}
}
Expand Down
56 changes: 56 additions & 0 deletions internal/ls/converters_bounds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ls

import (
"testing"

"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
)

type mockScript struct {
fileName string
text string
}

func (m *mockScript) FileName() string {
return m.fileName
}

func (m *mockScript) Text() string {
return m.text
}

func TestPositionToLineAndCharacterBoundsCheck(t *testing.T) {
t.Parallel()

// Test case that reproduces the panic with multi-byte characters and trailing newlines
text := "\"β†’\" ;\n\n\n"

lineMap := ComputeLineStarts(text)

converters := NewConverters(lsproto.PositionEncodingKindUTF16, func(fileName string) *LineMap {
return lineMap
})

script := &mockScript{
fileName: "test.ts",
text: text,
}

// This should not panic even if position is beyond text length
textLen := len(text)
position := core.TextPos(textLen) // position at end of text

// This should work
result := converters.PositionToLineAndCharacter(script, position)

t.Logf("Text length: %d, Position: %d", textLen, position)
t.Logf("Result: line=%d, char=%d", result.Line, result.Character)

// This should also not panic, even though position is beyond text length
beyondEndPosition := core.TextPos(textLen + 1)
result2 := converters.PositionToLineAndCharacter(script, beyondEndPosition)

t.Logf("Beyond end position: %d", beyondEndPosition)
t.Logf("Result2: line=%d, char=%d", result2.Line, result2.Character)
}
Loading