types/known/anypb: gracefully handle nil sources

Add checks so that MarshalFrom and UnmarshalTo consistently matches
the behavior of the proto package with regard to nil messages.
In particular, using a nil message as the destination results in
a panic (which is already the current behavior), while using a
nil message as the source does not panic (it is usually treated as
an untyped empty message). Since an untyped message has no
sensible meaning in the context of Any, return an error.

Change-Id: I99e86c2cdfbd8be57cc039efd550458dc56aadbc
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/237920
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/cmd/protoc-gen-go/internal_gengo/well_known_types.go b/cmd/protoc-gen-go/internal_gengo/well_known_types.go
index 99f3e44..4fc1cd7 100644
--- a/cmd/protoc-gen-go/internal_gengo/well_known_types.go
+++ b/cmd/protoc-gen-go/internal_gengo/well_known_types.go
@@ -25,6 +25,9 @@
 		g.P("// If no options are specified, call dst.MarshalFrom instead.")
 		g.P("func MarshalFrom(dst *Any, src ", protoPackage.Ident("Message"), ", opts ", protoPackage.Ident("MarshalOptions"), ") error {")
 		g.P("	const urlPrefix = \"type.googleapis.com/\"")
+		g.P("	if src == nil {")
+		g.P("		return ", protoimplPackage.Ident("X"), ".NewError(\"invalid nil source message\")")
+		g.P("	}")
 		g.P("	b, err := opts.Marshal(src)")
 		g.P("	if err != nil {")
 		g.P("		return err")
@@ -41,6 +44,9 @@
 		g.P("//")
 		g.P("// If no options are specified, call src.UnmarshalTo instead.")
 		g.P("func UnmarshalTo(src *Any, dst ", protoPackage.Ident("Message"), ", opts ", protoPackage.Ident("UnmarshalOptions"), ") error {")
+		g.P("	if src.GetTypeUrl() == \"\" {")
+		g.P("		return ", protoimplPackage.Ident("X"), ".NewError(\"invalid empty type URL\")")
+		g.P("	}")
 		g.P("	if !src.MessageIs(dst) {")
 		g.P("		got := dst.ProtoReflect().Descriptor().FullName()")
 		g.P("		want := src.MessageName()")
diff --git a/types/known/anypb/any.pb.go b/types/known/anypb/any.pb.go
index 775d839..4a7f5bb 100644
--- a/types/known/anypb/any.pb.go
+++ b/types/known/anypb/any.pb.go
@@ -167,6 +167,9 @@
 // If no options are specified, call dst.MarshalFrom instead.
 func MarshalFrom(dst *Any, src proto.Message, opts proto.MarshalOptions) error {
 	const urlPrefix = "type.googleapis.com/"
+	if src == nil {
+		return protoimpl.X.NewError("invalid nil source message")
+	}
 	b, err := opts.Marshal(src)
 	if err != nil {
 		return err
@@ -182,6 +185,9 @@
 //
 // If no options are specified, call src.UnmarshalTo instead.
 func UnmarshalTo(src *Any, dst proto.Message, opts proto.UnmarshalOptions) error {
+	if src.GetTypeUrl() == "" {
+		return protoimpl.X.NewError("invalid empty type URL")
+	}
 	if !src.MessageIs(dst) {
 		got := dst.ProtoReflect().Descriptor().FullName()
 		want := src.MessageName()