Skip to content

Commit b627338

Browse files
authored
go bindings: Don't panic on half-finished descriptors (#171)
1 parent 231eb01 commit b627338

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

bindings/go/scip/symbol.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ func (s *symbolParser) error(message string) error {
9696
}
9797

9898
func (s *symbolParser) current() rune {
99-
return s.Symbol[s.index]
99+
if s.index < len(s.Symbol) {
100+
return s.Symbol[s.index]
101+
}
102+
return '\x00'
100103
}
101104

102105
func (s *symbolParser) peekNext() rune {
@@ -119,6 +122,7 @@ func (s *symbolParser) parseDescriptors() ([]*Descriptor, error) {
119122
}
120123

121124
func (s *symbolParser) parseDescriptor() (*Descriptor, error) {
125+
start := s.index
122126
switch s.peekNext() {
123127
case '(':
124128
s.index++
@@ -168,7 +172,12 @@ func (s *symbolParser) parseDescriptor() (*Descriptor, error) {
168172
default:
169173
}
170174
}
171-
return nil, nil
175+
176+
end := s.index
177+
if s.index > len(s.Symbol) {
178+
end = len(s.Symbol)
179+
}
180+
return nil, errors.Newf("unrecognized descriptor %q", string(s.Symbol[start:end]))
172181
}
173182

174183
func (s *symbolParser) acceptIdentifier(what string) (string, error) {

bindings/go/scip/symbol_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,16 @@ func TestParseSymbol(t *testing.T) {
8181
})
8282
}
8383
}
84+
85+
func TestParseSymbolNoCrash(t *testing.T) {
86+
for _, symbolName := range []string{
87+
"lsif-java maven package 1.0.0",
88+
"lsif-java maven package 1.0.0 java/io/File#Entry.trailingstring",
89+
"lsif-java maven package 1.0.0 java/io/File#Entry.unrecognizedSuffix@",
90+
} {
91+
92+
if _, err := ParseSymbol(symbolName); err == nil {
93+
t.Fatalf("expected error from parsing %q", symbolName)
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)