Skip to content

Commit

Permalink
Catch panics during deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Nov 30, 2023
1 parent d566ee3 commit aa1ec10
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions types/serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit aa1ec10

Please sign in to comment.