diff --git a/codec/dagcbor/roundtrip_test.go b/codec/dagcbor/roundtrip_test.go index ada6c508..d897611c 100644 --- a/codec/dagcbor/roundtrip_test.go +++ b/codec/dagcbor/roundtrip_test.go @@ -161,10 +161,10 @@ func TestInts(t *testing.T) { qt.Assert(t, ii, qt.Equals, int64(td.intValue)) } - // if it's a positive number, we should be able to access it as a UintNode - // and be able to access the full int64 range + // if the number is outside of the positive int64 range, we should be able + // to access it as a UintNode and be able to access the full int64 range uin, ok := n.(datamodel.UintNode) - if td.intValue < 0 { + if td.value <= math.MaxInt64 { qt.Assert(t, ok, qt.IsFalse) } else { qt.Assert(t, ok, qt.IsTrue) diff --git a/codec/dagcbor/unmarshal.go b/codec/dagcbor/unmarshal.go index 982ad7fe..37d72cc7 100644 --- a/codec/dagcbor/unmarshal.go +++ b/codec/dagcbor/unmarshal.go @@ -276,7 +276,12 @@ func unmarshal2(na datamodel.NodeAssembler, tokSrc shared.TokenSource, tk *tok.T if *gas < 0 { return ErrAllocationBudgetExceeded } - return na.AssignNode(basicnode.NewUint(tk.Uint)) + // note that this pushes any overflow errors up the stack when AsInt() may + // be called on a UintNode that is too large to cast to an int64 + if tk.Uint > math.MaxInt64 { + return na.AssignNode(basicnode.NewUint(tk.Uint)) + } + return na.AssignInt(int64(tk.Uint)) case tok.TFloat64: *gas -= 1 if *gas < 0 {