proto: fix equality on nil values of different types

Equal((*M1)(nil), (*M2)(nil)) should be false.

Change-Id: I7def8016fcf1e78d9e69f79c23ab44fc3d211bb0
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/213479
Reviewed-by: Joe Tsai <joetsai@google.com>
This commit is contained in:
Damien Neil 2020-01-06 15:12:19 -08:00
parent 2244bc5833
commit 2ad3f248e2
2 changed files with 17 additions and 2 deletions

View File

@ -35,8 +35,8 @@ func Equal(x, y Message) bool {
}
mx := x.ProtoReflect()
my := y.ProtoReflect()
if !mx.IsValid() || !my.IsValid() {
return !mx.IsValid() && !my.IsValid()
if mx.IsValid() != my.IsValid() {
return false
}
return equalMessage(mx, my)
}

View File

@ -41,6 +41,21 @@ func TestEqual(t *testing.T) {
y: new(testpb.TestAllTypes),
eq: true,
},
{
x: (*testpb.TestAllTypes)(nil),
y: (*testpb.TestAllExtensions)(nil),
eq: false,
},
{
x: (*testpb.TestAllTypes)(nil),
y: new(testpb.TestAllExtensions),
eq: false,
},
{
x: new(testpb.TestAllTypes),
y: new(testpb.TestAllExtensions),
eq: false,
},
// Proto2 scalars.
{