cmd/protoc-gen-go: fix Reset method
The MessageInfo cache, once set, must not be cleared, otherwise
there exists a *messageState value where the MessageInfo value is nil.
Fix the generation of the Reset method to avoid clearing this value.
Change-Id: Ic84ca8b2640a43e967c36993da1ccd3f2b7096c4
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/201478
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/message_reflect_test.go b/internal/impl/message_reflect_test.go
index 9026e6e..c5a4a6a 100644
--- a/internal/impl/message_reflect_test.go
+++ b/internal/impl/message_reflect_test.go
@@ -1416,6 +1416,19 @@
return strings.Join(ss, ".")
}
+func TestReset(t *testing.T) {
+ mi := new(testpb.TestAllTypes)
+
+ // ProtoReflect is implemented using a messageState cache.
+ m := mi.ProtoReflect()
+
+ // Reset must not clear the messageState cache.
+ mi.Reset()
+
+ // If Reset accidentally cleared the messageState cache, this panics.
+ m.Descriptor()
+}
+
// The MessageState implementation makes the assumption that when a
// concrete message is unsafe casted as a *MessageState, the Go GC does
// not reclaim the memory for the remainder of the concrete message.