From 91a6a2a30f95da8ae3fb6329a71c49ed13aa12ad Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 5 Dec 2017 22:53:48 -0800 Subject: encoding/json: make error capture logic in recover more type safe Rather than only ignoring runtime.Error panics, which are a very narrow set of possible panic values, switch it such that the json package only captures panic values that have been properly wrapped in a jsonError struct. This ensures that only intentional panics originating from the json package are captured. Fixes #23012 Change-Id: I5e85200259edd2abb1b0512ce6cc288849151a6d Reviewed-on: https://go-review.googlesource.com/94019 Run-TryBot: Joe Tsai TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/encoding/json/decode_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/encoding/json/decode_test.go') diff --git a/src/encoding/json/decode_test.go b/src/encoding/json/decode_test.go index 34b7ec6d97..90fdf93dbd 100644 --- a/src/encoding/json/decode_test.go +++ b/src/encoding/json/decode_test.go @@ -2166,3 +2166,17 @@ func TestUnmarshalEmbeddedPointerUnexported(t *testing.T) { } } } + +type unmarshalPanic struct{} + +func (unmarshalPanic) UnmarshalJSON([]byte) error { panic(0xdead) } + +func TestUnmarshalPanic(t *testing.T) { + defer func() { + if got := recover(); !reflect.DeepEqual(got, 0xdead) { + t.Errorf("panic() = (%T)(%v), want 0xdead", got, got) + } + }() + Unmarshal([]byte("{}"), &unmarshalPanic{}) + t.Fatalf("Unmarshal should have panicked") +} -- cgit v1.3