From: Emmanuel Odeke Date: Sun, 17 Jul 2016 21:22:15 +0000 (-0700) Subject: encoding/gob: error out instead of panicking on nil dereference X-Git-Tag: go1.8beta1~1763 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0a2a64d85d52ad51de34d39bc5685c39c0e1e32a;p=gostls13.git encoding/gob: error out instead of panicking on nil dereference Do not panic when we encounter nil interface values which are invalid values for gob. Previously this wasn't caught yet we were calling reflect.*.Type() on reflect.Invalid values thereby causing panic: `panic: reflect: call of reflect.Value.Type on zero Value.` which is a panic not enforced by encoding/gob itself. We can catch this and send back an error to the caller. Fixes #16204 Change-Id: Ie646796db297759a74a02eee5267713adbe0c3a0 Reviewed-on: https://go-review.googlesource.com/24989 Reviewed-by: Rob Pike Run-TryBot: Rob Pike --- diff --git a/src/encoding/gob/encoder.go b/src/encoding/gob/encoder.go index d6c8fdd963..40ec81b6e6 100644 --- a/src/encoding/gob/encoder.go +++ b/src/encoding/gob/encoder.go @@ -215,6 +215,9 @@ func (enc *Encoder) sendTypeId(state *encoderState, ut *userTypeInfo) { // guaranteeing that all necessary type information has been transmitted first. // Passing a nil pointer to EncodeValue will panic, as they cannot be transmitted by gob. func (enc *Encoder) EncodeValue(value reflect.Value) error { + if value.Kind() == reflect.Invalid { + return errors.New("gob: cannot encode nil value") + } if value.Kind() == reflect.Ptr && value.IsNil() { panic("gob: cannot encode nil pointer of type " + value.Type().String()) } diff --git a/src/encoding/gob/encoder_test.go b/src/encoding/gob/encoder_test.go index 22090a18a6..9256848b50 100644 --- a/src/encoding/gob/encoder_test.go +++ b/src/encoding/gob/encoder_test.go @@ -830,6 +830,20 @@ func TestPtrToMapOfMap(t *testing.T) { } } +// Test that untyped nils generate an error, not a panic. +// See Issue 16204. +func TestCatchInvalidNilValue(t *testing.T) { + encodeErr, panicErr := encodeAndRecover(nil) + if panicErr != nil { + t.Fatalf("panicErr=%v, should not panic encoding untyped nil", panicErr) + } + if encodeErr == nil { + t.Errorf("got err=nil, want non-nil error when encoding untyped nil value") + } else if !strings.Contains(encodeErr.Error(), "nil value") { + t.Errorf("expected 'nil value' error; got err=%v", encodeErr) + } +} + // A top-level nil pointer generates a panic with a helpful string-valued message. func TestTopLevelNilPointer(t *testing.T) { var ip *int