Skip to content

Commit

Permalink
Merge pull request #354 from blinklabs-io/fix/cbor-value-panic
Browse files Browse the repository at this point in the history
fix: recover panics when decoding CBOR maps
  • Loading branch information
agaffney authored Jun 30, 2023
2 parents 101e68d + 094a255 commit 05743ac
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cbor/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package cbor

import (
"fmt"
)

// Helpful wrapper for parsing arbitrary CBOR data which may contain types that
// cannot be easily represented in Go (such as maps with bytestring keys)
type Value struct {
Expand All @@ -22,12 +26,20 @@ type Value struct {
cborData string
}

func (v *Value) UnmarshalCBOR(data []byte) error {
func (v *Value) UnmarshalCBOR(data []byte) (err error) {
// Save the original CBOR
v.cborData = string(data[:])
cborType := data[0] & CBOR_TYPE_MASK
switch cborType {
case CBOR_TYPE_MAP:
// There are certain types that cannot be used as map keys in Go but are valid in CBOR. Trying to
// parse CBOR containing a map with keys of one of those types will cause a panic. We setup this
// deferred function to recover from a possible panic and return an error
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("decode failure, probably due to type unsupported by Go: %v", r)
}
}()
tmpValue := map[Value]Value{}
if _, err := Decode(data, &tmpValue); err != nil {
return err
Expand Down

0 comments on commit 05743ac

Please sign in to comment.