reflect/protodesc: don't panic on nil Resolver

The change to make protodesc.NewFile take an interface rather than a
concrete type means that NewFile(f, nil) now causes a panic. (A nil
*protoregistry.Files is valid.)

Fix this panic by using a default, empty registry when NewFile's second
parameter is nil.

Change-Id: I70a1f0759e7ea5b57fba5b6123ee85188f4d560c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/182979
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
This commit is contained in:
Damien Neil 2019-06-19 10:21:17 -07:00
parent 5857a5ab4f
commit 3631e225f8

View File

@ -13,6 +13,7 @@ import (
"google.golang.org/protobuf/internal/errors"
"google.golang.org/protobuf/internal/prototype"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
)
@ -53,6 +54,9 @@ type Resolver interface {
// The caller must relinquish full ownership of the input fd and must not
// access or mutate any fields.
func NewFile(fd *descriptorpb.FileDescriptorProto, r Resolver) (protoreflect.FileDescriptor, error) {
if r == nil {
r = (*protoregistry.Files)(nil) // empty resolver
}
var f prototype.File
switch fd.GetSyntax() {
case "proto2", "":