Skip to content

Commit 4c14bcb

Browse files
committed
fix: better handling for arbitrary CBOR data structures
* use a pointer for map keys for non-comparable types in cbor.Value * allow arbitrary tag types for cbor.Value Signed-off-by: Aurora Gaffney <[email protected]>
1 parent bd5cb0d commit 4c14bcb

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

cbor/value.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"math/big"
22+
"reflect"
2223
"sort"
2324
"strings"
2425
)
@@ -70,16 +71,11 @@ func (v *Value) UnmarshalCBOR(data []byte) error {
7071
v.value = tmpConstr
7172
} else {
7273
// Fall back to standard CBOR tag parsing for our supported types
73-
var tmpTagDecode interface{}
74+
var tmpTagDecode any
7475
if _, err := Decode(data, &tmpTagDecode); err != nil {
7576
return err
7677
}
77-
switch tmpTagDecode.(type) {
78-
case int, uint, int64, uint64, bool, big.Int, WrappedCbor, Rat, Set, Map:
79-
v.value = tmpTagDecode
80-
default:
81-
return fmt.Errorf("unsupported CBOR tag number: %d", tmpTag.Number)
82-
}
78+
v.value = tmpTagDecode
8379
}
8480
default:
8581
var tmpValue interface{}
@@ -132,14 +128,19 @@ func (v *Value) processMap(data []byte) (err error) {
132128
)
133129
}
134130
}()
135-
tmpValue := map[Value]Value{}
131+
tmpValue := map[*Value]Value{}
136132
if _, err = Decode(data, &tmpValue); err != nil {
137133
return err
138134
}
139135
// Extract actual value from each child value
140-
newValue := map[interface{}]interface{}{}
136+
newValue := map[any]any{}
141137
for key, value := range tmpValue {
142-
newValue[key.Value()] = value.Value()
138+
keyValue := key.Value()
139+
// Use a pointer for unhashable key types
140+
if !reflect.TypeOf(keyValue).Comparable() {
141+
keyValue = &keyValue
142+
}
143+
newValue[keyValue] = value.Value()
143144
}
144145
v.value = newValue
145146
return nil
@@ -151,7 +152,7 @@ func (v *Value) processArray(data []byte) error {
151152
return err
152153
}
153154
// Extract actual value from each child value
154-
newValue := []interface{}{}
155+
newValue := []any{}
155156
for _, value := range tmpValue {
156157
newValue = append(newValue, value.Value())
157158
}

cbor/value_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ var testDefs = []struct {
4646
expectedObject: nil,
4747
expectedDecodeError: io.ErrUnexpectedEOF,
4848
},
49-
// Invalid map key type
50-
{
51-
cborHex: "A1810000",
52-
expectedDecodeError: fmt.Errorf(
53-
"decode failure, probably due to type unsupported by Go: runtime error: hash of unhashable type []interface {}",
54-
),
55-
},
5649
// [1, 2, 3]
5750
{
5851
cborHex: "83010203",

0 commit comments

Comments
 (0)