Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: recover panics when decoding CBOR maps #354

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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