From aa1ec10a3e92c704f2c3c101fac9216945cf5bd0 Mon Sep 17 00:00:00 2001 From: Chris O'Hara Date: Fri, 1 Dec 2023 09:10:01 +1000 Subject: [PATCH] Catch panics during deserialization --- types/serde.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/types/serde.go b/types/serde.go index 73f87f8..516d690 100644 --- a/types/serde.go +++ b/types/serde.go @@ -66,7 +66,14 @@ func Serialize(x any) ([]byte, error) { } // Deserialize value from b. Return left over bytes. -func Deserialize(b []byte) (interface{}, error) { +func Deserialize(b []byte) (x interface{}, err error) { + defer func() { + // FIXME: the deserialize*() functions panic on invalid input + if e := recover(); e != nil { + err = fmt.Errorf("cannot deserialize state: %v", e) + } + }() + var state coroutinev1.State if err := state.UnmarshalVT(b); err != nil { return nil, err @@ -77,16 +84,15 @@ func Deserialize(b []byte) (interface{}, error) { d := newDeserializer(state.Root.Data, state.Types, state.Functions, state.Regions, state.Strings) - var x interface{} px := &x t := reflect.TypeOf(px).Elem() p := unsafe.Pointer(px) deserializeInterface(d, t, p) if len(d.b) != 0 { - return nil, errors.New("trailing bytes") + err = errors.New("trailing bytes") } - return x, nil + return } type Deserializer struct {