From 151d2c4cdf34cbfddda07782b49cab09465233ae Mon Sep 17 00:00:00 2001 From: "Ilya (Marshal)" Date: Tue, 17 Jun 2025 22:24:06 +0200 Subject: [PATCH 1/2] Fix smallest CBOR integer --- pytests/test_dag_cbor.py | 10 ++++++++++ src/lib.rs | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pytests/test_dag_cbor.py b/pytests/test_dag_cbor.py index f099e3f..c509d4c 100644 --- a/pytests/test_dag_cbor.py +++ b/pytests/test_dag_cbor.py @@ -135,3 +135,13 @@ def test_recursion_limit_exceed_on_nested_maps() -> None: libipld.decode_dag_cbor(dag_cbor) assert 'in DAG-CBOR decoding' in str(exc_info.value) + + +def test_dag_cbor_decode_largest_unsigned_int() -> None: + result = libipld.decode_dag_cbor(bytes.fromhex('1bffffffffffffffff')) + assert result == 2**64 - 1 + + +def test_dag_cbor_decode_smallest_negative_int() -> None: + result = libipld.decode_dag_cbor(bytes.fromhex('3bffffffffffffffff')) + assert result == -(2**64) diff --git a/src/lib.rs b/src/lib.rs index 290eb2d..aeb80db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,7 +122,7 @@ fn decode_dag_cbor_to_pyobject( let major = decode::read_major(r)?; Ok(match major.kind() { MajorKind::UnsignedInt => decode::read_uint(r, major)?.into_pyobject(py)?.into(), - MajorKind::NegativeInt => (-1 - decode::read_uint(r, major)? as i64).into_pyobject(py)?.into(), + MajorKind::NegativeInt => (-1 - decode::read_uint(r, major)? as i128).into_pyobject(py)?.into(), MajorKind::ByteString => { let len = decode::read_uint(r, major)?; PyBytes::new(py, &decode::read_bytes(r, len)?).into_pyobject(py)?.into() From eb544d40eb81a4ff02f729c5e656d1064ff7050a Mon Sep 17 00:00:00 2001 From: "Ilya (Marshal)" Date: Tue, 17 Jun 2025 23:06:47 +0200 Subject: [PATCH 2/2] fix largest unsigned int roundtrip --- pytests/test_dag_cbor.py | 22 ++++++++++++++++------ src/lib.rs | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pytests/test_dag_cbor.py b/pytests/test_dag_cbor.py index c509d4c..4169354 100644 --- a/pytests/test_dag_cbor.py +++ b/pytests/test_dag_cbor.py @@ -137,11 +137,21 @@ def test_recursion_limit_exceed_on_nested_maps() -> None: assert 'in DAG-CBOR decoding' in str(exc_info.value) -def test_dag_cbor_decode_largest_unsigned_int() -> None: - result = libipld.decode_dag_cbor(bytes.fromhex('1bffffffffffffffff')) - assert result == 2**64 - 1 +def test_dag_cbor_decode_largest_unsigned_int_roundtrip() -> None: + data = bytes.fromhex('1bffffffffffffffff') + decoded_result = libipld.decode_dag_cbor(data) + assert decoded_result == 2**64 - 1 -def test_dag_cbor_decode_smallest_negative_int() -> None: - result = libipld.decode_dag_cbor(bytes.fromhex('3bffffffffffffffff')) - assert result == -(2**64) + encoded_result = libipld.encode_dag_cbor(decoded_result) + assert encoded_result == data + + +def test_dag_cbor_decode_smallest_negative_int_roundtrip() -> None: + data = bytes.fromhex('3bffffffffffffffff') + + decoded_result = libipld.decode_dag_cbor(data) + assert decoded_result == -(2**64) + + encoded_result = libipld.encode_dag_cbor(decoded_result) + assert encoded_result == data diff --git a/src/lib.rs b/src/lib.rs index aeb80db..367b02a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -231,7 +231,7 @@ fn encode_dag_cbor_from_pyobject<'py, W: Write>( Ok(()) } else if obj.is_instance_of::() { - let i: i64 = obj.extract()?; + let i: i128 = obj.extract()?; if i.is_negative() { encode::write_u64(w, MajorKind::NegativeInt, -(i + 1) as u64)?