Skip to content

Commit

Permalink
go bindings: Don't panic on half-finished descriptors (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
efritz authored Jun 13, 2023
1 parent 231eb01 commit b627338
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 11 additions & 2 deletions bindings/go/scip/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ func (s *symbolParser) error(message string) error {
}

func (s *symbolParser) current() rune {
return s.Symbol[s.index]
if s.index < len(s.Symbol) {
return s.Symbol[s.index]
}
return '\x00'
}

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

func (s *symbolParser) parseDescriptor() (*Descriptor, error) {
start := s.index
switch s.peekNext() {
case '(':
s.index++
Expand Down Expand Up @@ -168,7 +172,12 @@ func (s *symbolParser) parseDescriptor() (*Descriptor, error) {
default:
}
}
return nil, nil

end := s.index
if s.index > len(s.Symbol) {
end = len(s.Symbol)
}
return nil, errors.Newf("unrecognized descriptor %q", string(s.Symbol[start:end]))
}

func (s *symbolParser) acceptIdentifier(what string) (string, error) {
Expand Down
13 changes: 13 additions & 0 deletions bindings/go/scip/symbol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,16 @@ func TestParseSymbol(t *testing.T) {
})
}
}

func TestParseSymbolNoCrash(t *testing.T) {
for _, symbolName := range []string{
"lsif-java maven package 1.0.0",
"lsif-java maven package 1.0.0 java/io/File#Entry.trailingstring",
"lsif-java maven package 1.0.0 java/io/File#Entry.unrecognizedSuffix@",
} {

if _, err := ParseSymbol(symbolName); err == nil {
t.Fatalf("expected error from parsing %q", symbolName)
}
}
}

0 comments on commit b627338

Please sign in to comment.