Skip to content

Commit 341f947

Browse files
committed
fix: add bounds checking function to prevent slice out of range panic
1 parent e8e3c41 commit 341f947

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

internal/ls/autoimports.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
"github.com/microsoft/typescript-go/internal/tspath"
2222
)
2323

24+
// isValidSliceRange checks if the given start and end indices are valid for slicing a string/slice of the given length.
25+
func isValidSliceRange(start, end, length int) bool {
26+
return start >= 0 && end >= 0 && start <= end && end <= length
27+
}
28+
2429
type SymbolExportInfo struct {
2530
symbol *ast.Symbol
2631
moduleSymbol *ast.Symbol
@@ -120,7 +125,7 @@ func (e *exportInfoMap) add(
120125

121126
// Bounds check to prevent slice bounds out of range panic
122127
fileName := moduleFile.FileName()
123-
if topLevelPackageNameIndex+1 >= 0 && packageRootIndex >= 0 && topLevelPackageNameIndex+1 <= packageRootIndex && packageRootIndex <= len(fileName) {
128+
if isValidSliceRange(topLevelPackageNameIndex+1, packageRootIndex, len(fileName)) {
124129
packageName = module.UnmangleScopedPackageName(modulespecifiers.GetPackageNameFromTypesPackageName(fileName[topLevelPackageNameIndex+1 : packageRootIndex]))
125130
}
126131

0 commit comments

Comments
 (0)