Skip to content

Commit

Permalink
Merge pull request #114 from jhump/jh/fix-bugs-in-protoparse
Browse files Browse the repository at this point in the history
fix nil-deref bug; improve error messages; fix confusing comment in protoprint
  • Loading branch information
jhump authored Mar 9, 2018
2 parents 19350c2 + 58bab37 commit 6f4f3bb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions desc/protoparse/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,12 @@ type fieldNode struct {
}

func (n *fieldNode) fieldLabel() node {
// proto3 fields and fields inside one-ofs will not have a label and we need
// this check in order to return a nil node -- otherwise we'd return a
// non-nil node that has a nil pointer value in it :/
if n.label == nil {
return nil
}
return n.label
}

Expand Down
43 changes: 43 additions & 0 deletions desc/protoparse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,49 @@ import (

func init() {
protoErrorVerbose = true

// fix up the generated "token name" array so that error messages are nicer
setTokenName(_STRING_LIT, "string literal")
setTokenName(_INT_LIT, "int literal")
setTokenName(_FLOAT_LIT, "float literal")
setTokenName(_NAME, "identifier")
setTokenName(_FQNAME, "fully-qualified name")
setTokenName(_TYPENAME, "type name")
setTokenName(_TYPENAME, "error")
// for keywords, just show the keyword itself wrapped in quotes
for str, i := range keywords {
setTokenName(i, fmt.Sprintf(`"%s"`, str))
}
}

func setTokenName(token int, text string) {
// NB: this is based on logic in generated parse code that translates the
// int returned from the lexer into an internal token number.
var intern int
if token < len(protoTok1) {
intern = protoTok1[token]
} else {
if token >= protoPrivate {
if token < protoPrivate+len(protoTok2) {
intern = protoTok2[token-protoPrivate]
}
}
if intern == 0 {
for i := 0; i+1 < len(protoTok3); i += 2 {
if protoTok3[i] == token {
intern = protoTok3[i+1]
break
}
}
}
}

if intern >= 1 && intern-1 < len(protoToknames) {
protoToknames[intern-1] = text
return
}

panic(fmt.Sprintf("Unknown token value: %d", token))
}

// FileAccessor is an abstraction for opening proto source files. It takes the
Expand Down
2 changes: 1 addition & 1 deletion desc/protoprint/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (p *Printer) printMessageBody(md *desc.MessageDescriptor, mf *dynamic.Messa
for i, el := range elements.addrs {
d := elements.at(el)
// skip[d] will panic if d is a slice (which it could be for []option),
// so just ignore since don't try to skip options
// so just ignore it since we don't try to skip options
if reflect.TypeOf(d).Kind() != reflect.Slice && skip[d] {
// skip this element
continue
Expand Down

0 comments on commit 6f4f3bb

Please sign in to comment.