Return a descriptive error when a map field has a nil element.
I'm not sure if this matches C++, but it makes sense and otherwise
the output is not decodable by this same package. It also parallels
our rejection of nil elements of a repeated field.
diff --git a/proto/all_test.go b/proto/all_test.go
index 8784187..5a9b6a4 100644
--- a/proto/all_test.go
+++ b/proto/all_test.go
@@ -1925,6 +1925,18 @@
}
}
+func TestMapFieldWithNil(t *testing.T) {
+ m := &MessageWithMap{
+ MsgMapping: map[int64]*FloatingPoint{
+ 1: nil,
+ },
+ }
+ b, err := Marshal(m)
+ if err == nil {
+ t.Fatalf("Marshal of bad map should have failed, got these bytes: %v", b)
+ }
+}
+
// Benchmarks
func testMsg() *GoTest {
diff --git a/proto/encode.go b/proto/encode.go
index d1abc33..b46f760 100644
--- a/proto/encode.go
+++ b/proto/encode.go
@@ -1106,6 +1106,11 @@
for _, key := range keys {
val := v.MapIndex(key)
+ // The only illegal map entry values are nil message pointers.
+ if val.Kind() == reflect.Ptr && val.IsNil() {
+ return errors.New("proto: map has nil element")
+ }
+
keycopy.Set(key)
valcopy.Set(val)