Commit 0a2a64d8 authored by Emmanuel Odeke's avatar Emmanuel Odeke Committed by Rob Pike

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/24989Reviewed-by: 's avatarRob Pike <r@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
parent 14e59511
......@@ -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())
}
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment