Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nil-deref bug; improve error messages; fix confusing comment in protoprint #114

Merged
merged 1 commit into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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