File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff line change 1414
1515package cbor
1616
17+ import (
18+ "fmt"
19+ )
20+
1721// Helpful wrapper for parsing arbitrary CBOR data which may contain types that
1822// cannot be easily represented in Go (such as maps with bytestring keys)
1923type Value struct {
@@ -22,12 +26,20 @@ type Value struct {
2226 cborData string
2327}
2428
25- func (v * Value ) UnmarshalCBOR (data []byte ) error {
29+ func (v * Value ) UnmarshalCBOR (data []byte ) ( err error ) {
2630 // Save the original CBOR
2731 v .cborData = string (data [:])
2832 cborType := data [0 ] & CBOR_TYPE_MASK
2933 switch cborType {
3034 case CBOR_TYPE_MAP :
35+ // There are certain types that cannot be used as map keys in Go but are valid in CBOR. Trying to
36+ // parse CBOR containing a map with keys of one of those types will cause a panic. We setup this
37+ // deferred function to recover from a possible panic and return an error
38+ defer func () {
39+ if r := recover (); r != nil {
40+ err = fmt .Errorf ("decode failure, probably due to type unsupported by Go: %v" , r )
41+ }
42+ }()
3143 tmpValue := map [Value ]Value {}
3244 if _ , err := Decode (data , & tmpValue ); err != nil {
3345 return err
You can’t perform that action at this time.
0 commit comments