Skip to content

Commit 620d469

Browse files
arash-mosaviclaude
andcommitted
Add comprehensive bounds checking to prevent slice panics in autoimports
- Add bounds check before accessing names[0] when symbol names array may be empty - Add bounds checking in getNodeModuleRootSpecifier for component array access - Prevent panics when accessing array elements without verifying length Fixes #1691 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent c3c40e2 commit 620d469

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

internal/ls/autoimports.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func (e *exportInfoMap) add(
164164
names = getNamesForExportedSymbol(namedSymbol, ch, core.ScriptTargetNone)
165165
}
166166

167+
// Bounds check to prevent slice bounds out of range panic
168+
if len(names) == 0 {
169+
return
170+
}
167171
symbolName := names[0]
168172
if symbolNameMatch != nil && !symbolNameMatch(symbolName) {
169173
return
@@ -674,12 +678,23 @@ func (l *LanguageService) createPackageJsonImportFilter(fromFile *ast.SourceFile
674678
sourceFileCache := map[*ast.SourceFile]packageJsonFilterResult{}
675679

676680
getNodeModuleRootSpecifier := func(fullSpecifier string) string {
677-
components := tspath.GetPathComponents(modulespecifiers.GetPackageNameFromTypesPackageName(fullSpecifier), "")[1:]
681+
components := tspath.GetPathComponents(modulespecifiers.GetPackageNameFromTypesPackageName(fullSpecifier), "")
682+
// Bounds check to prevent slice bounds out of range panic
683+
if len(components) < 2 {
684+
return ""
685+
}
686+
components = components[1:]
678687
// Scoped packages
679-
if strings.HasPrefix(components[0], "@") {
688+
if len(components) > 0 && strings.HasPrefix(components[0], "@") {
689+
if len(components) < 2 {
690+
return components[0]
691+
}
680692
return fmt.Sprintf("%s/%s", components[0], components[1])
681693
}
682-
return components[0]
694+
if len(components) > 0 {
695+
return components[0]
696+
}
697+
return ""
683698
}
684699

685700
moduleSpecifierIsCoveredByPackageJson := func(specifier string) bool {

0 commit comments

Comments
 (0)