From e87cf53d57c90a30ac96ee468bd46f3f68b1d4f0 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 10 Sep 2019 12:20:00 -0700 Subject: [PATCH] internal/impl: fix panic calling XXX_MessageName on aberrant messages Call XXX_MessageName with best-effort. If it panics, oh-well. Change-Id: I605ea074470b0c90b0bea8b36fa7d4a69368692d Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/194598 Reviewed-by: Damien Neil --- internal/impl/legacy_message.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/impl/legacy_message.go b/internal/impl/legacy_message.go index ff1c69bc..6c522848 100644 --- a/internal/impl/legacy_message.go +++ b/internal/impl/legacy_message.go @@ -202,10 +202,14 @@ func aberrantDeriveMessageName(t reflect.Type, name pref.FullName) pref.FullName if name.IsValid() { return name } - if m, ok := reflect.New(t).Interface().(interface{ XXX_MessageName() string }); ok { - if name := pref.FullName(m.XXX_MessageName()); name.IsValid() { - return name + func() { + defer func() { recover() }() // swallow possible nil panics + if m, ok := reflect.New(t).Interface().(interface{ XXX_MessageName() string }); ok { + name = pref.FullName(m.XXX_MessageName()) } + }() + if name.IsValid() { + return name } return aberrantDeriveFullName(t) }