Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pytests/test_dag_cbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,21 @@ def test_decode_dag_cbor_multi() -> None:
assert len(decoded) == 2
assert decoded[0] == 0
assert decoded[1] == 0


def test_encode_tag_positive_bignum() -> None:
bignum = 18446744073709551616

with pytest.raises(ValueError) as exc_info:
libipld.encode_dag_cbor(bignum)

assert 'number out of range' in str(exc_info.value).lower()


def test_encode_tag_negative_bignum() -> None:
bignum = -18446744073709551617

with pytest.raises(ValueError) as exc_info:
libipld.encode_dag_cbor(bignum)

assert 'number out of range' in str(exc_info.value).lower()
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,16 @@ fn encode_dag_cbor_from_pyobject<'py, W: Write>(
let i: i128 = obj.extract()?;

if i.is_negative() {
if -(i + 1) > u64::MAX as i128 {
return Err(anyhow!("Number out of range"));
}

encode::write_u64(w, MajorKind::NegativeInt, -(i + 1) as u64)?
} else {
if i > u64::MAX as i128 {
return Err(anyhow!("Number out of range"));
}

encode::write_u64(w, MajorKind::UnsignedInt, i as u64)?
}

Expand Down
Loading