diff options
| author | Emmanuel Odeke <emm.odeke@gmail.com> | 2016-07-17 14:22:15 -0700 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2016-08-19 21:54:39 +0000 |
| commit | 0a2a64d85d52ad51de34d39bc5685c39c0e1e32a (patch) | |
| tree | c42b4fe4ab44670f006550aaa27a4aff52910eee /src/encoding/gob/encoder_test.go | |
| parent | 14e59511661303ab1406f7c21ee27e58bcd0750e (diff) | |
| download | go-0a2a64d85d52ad51de34d39bc5685c39c0e1e32a.tar.xz | |
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 <r@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
Diffstat (limited to 'src/encoding/gob/encoder_test.go')
| -rw-r--r-- | src/encoding/gob/encoder_test.go | 14 |
1 files changed, 14 insertions, 0 deletions
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 |
