Skip to content

Commit c08e01a

Browse files
Fix panic in printer utilities when processing cross-file positions
Co-authored-by: RyanCavanaugh <[email protected]>
1 parent 1ae8c19 commit c08e01a

File tree

4 files changed

+23
-145
lines changed

4 files changed

+23
-145
lines changed

internal/printer/utilities.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,13 @@ func getStartPositionOfRange(r core.TextRange, sourceFile *ast.SourceFile, inclu
358358
if ast.PositionIsSynthesized(r.Pos()) {
359359
return -1
360360
}
361-
return scanner.SkipTriviaEx(sourceFile.Text(), r.Pos(), &scanner.SkipTriviaOptions{StopAtComments: includeComments})
361+
text := sourceFile.Text()
362+
pos := r.Pos()
363+
// Bounds check: if position is beyond text length, it's likely from a different file
364+
if pos >= len(text) {
365+
return pos // Return the original position without trying to skip trivia
366+
}
367+
return scanner.SkipTriviaEx(text, pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
362368
}
363369

364370
func positionsAreOnSameLine(pos1 int, pos2 int, sourceFile *ast.SourceFile) bool {
@@ -388,19 +394,31 @@ func getLinesBetweenRangeEndAndRangeStart(range1 core.TextRange, range2 core.Tex
388394
}
389395

390396
func getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(pos int, stopPos int, sourceFile *ast.SourceFile, includeComments bool) int {
391-
startPos := scanner.SkipTriviaEx(sourceFile.Text(), pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
397+
text := sourceFile.Text()
398+
if pos >= len(text) {
399+
return 0 // Can't determine line differences for out-of-bounds positions
400+
}
401+
startPos := scanner.SkipTriviaEx(text, pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
392402
prevPos := getPreviousNonWhitespacePosition(startPos, stopPos, sourceFile)
393403
return getLinesBetweenPositions(sourceFile, core.IfElse(prevPos >= 0, prevPos, stopPos), startPos)
394404
}
395405

396406
func getLinesBetweenPositionAndNextNonWhitespaceCharacter(pos int, stopPos int, sourceFile *ast.SourceFile, includeComments bool) int {
397-
nextPos := scanner.SkipTriviaEx(sourceFile.Text(), pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
407+
text := sourceFile.Text()
408+
if pos >= len(text) {
409+
return 0 // Can't determine line differences for out-of-bounds positions
410+
}
411+
nextPos := scanner.SkipTriviaEx(text, pos, &scanner.SkipTriviaOptions{StopAtComments: includeComments})
398412
return getLinesBetweenPositions(sourceFile, pos, core.IfElse(stopPos < nextPos, stopPos, nextPos))
399413
}
400414

401415
func getPreviousNonWhitespacePosition(pos int, stopPos int, sourceFile *ast.SourceFile) int {
416+
text := sourceFile.Text()
402417
for ; pos >= stopPos; pos-- {
403-
if !stringutil.IsWhiteSpaceLike(rune(sourceFile.Text()[pos])) {
418+
if pos >= len(text) {
419+
continue // Skip out-of-bounds positions
420+
}
421+
if !stringutil.IsWhiteSpaceLike(rune(text[pos])) {
404422
return pos
405423
}
406424
}

internal/scanner/issue1092_test.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

internal/scanner/scanner.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,9 +2094,6 @@ func SkipTriviaEx(text string, pos int, options *SkipTriviaOptions) int {
20942094
}
20952095

20962096
textLen := len(text)
2097-
if pos >= textLen {
2098-
return pos
2099-
}
21002097
canConsumeStar := false
21012098
// Keep in sync with couldStartTrivia
21022099
for {
@@ -2330,15 +2327,7 @@ func GetLineStarts(sourceFile ast.SourceFileLike) []core.TextPos {
23302327
func GetLineAndCharacterOfPosition(sourceFile ast.SourceFileLike, pos int) (line int, character int) {
23312328
lineMap := GetLineStarts(sourceFile)
23322329
line = ComputeLineOfPosition(lineMap, pos)
2333-
textLen := len(sourceFile.Text())
2334-
startPos := int(lineMap[line])
2335-
if pos > textLen {
2336-
pos = textLen
2337-
}
2338-
if startPos > textLen {
2339-
startPos = textLen
2340-
}
2341-
character = utf8.RuneCountInString(sourceFile.Text()[startPos:pos])
2330+
character = utf8.RuneCountInString(sourceFile.Text()[lineMap[line]:pos])
23422331
return
23432332
}
23442333

internal/scanner/scanner_test.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)