Skip to content

Commit 68d9395

Browse files
authored
fix: support larger num/denom for CBOR rational numbers (#792)
1 parent f2064fa commit 68d9395

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

cbor/tags.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,19 @@ type Rat struct {
9393
}
9494

9595
func (r *Rat) UnmarshalCBOR(cborData []byte) error {
96-
tmpRat := []int64{}
96+
tmpRat := []uint64{}
9797
if _, err := Decode(cborData, &tmpRat); err != nil {
9898
return err
9999
}
100-
r.Rat = big.NewRat(tmpRat[0], tmpRat[1])
100+
// Convert numerator and denominator to big.Int
101+
// It's necessary to do this to support num/denom larger than int64 (up to uint64)
102+
tmpNum := new(big.Int)
103+
tmpNum.SetUint64(tmpRat[0])
104+
tmpDenom := new(big.Int)
105+
tmpDenom.SetUint64(tmpRat[1])
106+
// Create new big.Rat with num/denom set to big.Int values above
107+
r.Rat = new(big.Rat)
108+
r.Rat.SetFrac(tmpNum, tmpDenom)
101109
return nil
102110
}
103111

cbor/tags_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ var tagsTestDefs = []struct {
5454
},
5555
),
5656
},
57+
{
58+
cborHex: "d81e821b80000000000000011b8ac7230489e80000",
59+
object: cbor.Rat{
60+
Rat: new(big.Rat).SetFrac(
61+
new(big.Int).SetUint64(9223372036854775809),
62+
new(big.Int).SetUint64(10000000000000000000),
63+
),
64+
},
65+
},
5766
}
5867

5968
func TestTagsDecode(t *testing.T) {

0 commit comments

Comments
 (0)