File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -246,3 +246,21 @@ def test_decode_dag_cbor_multi() -> None:
246246 assert len (decoded ) == 2
247247 assert decoded [0 ] == 0
248248 assert decoded [1 ] == 0
249+
250+
251+ def test_encode_tag_positive_bignum () -> None :
252+ bignum = 18446744073709551616
253+
254+ with pytest .raises (ValueError ) as exc_info :
255+ libipld .encode_dag_cbor (bignum )
256+
257+ assert 'number out of range' in str (exc_info .value ).lower ()
258+
259+
260+ def test_encode_tag_negative_bignum () -> None :
261+ bignum = - 18446744073709551617
262+
263+ with pytest .raises (ValueError ) as exc_info :
264+ libipld .encode_dag_cbor (bignum )
265+
266+ assert 'number out of range' in str (exc_info .value ).lower ()
Original file line number Diff line number Diff line change @@ -254,8 +254,16 @@ fn encode_dag_cbor_from_pyobject<'py, W: Write>(
254254 let i: i128 = obj. extract ( ) ?;
255255
256256 if i. is_negative ( ) {
257+ if -( i + 1 ) > u64:: MAX as i128 {
258+ return Err ( anyhow ! ( "Number out of range" ) ) ;
259+ }
260+
257261 encode:: write_u64 ( w, MajorKind :: NegativeInt , -( i + 1 ) as u64 ) ?
258262 } else {
263+ if i > u64:: MAX as i128 {
264+ return Err ( anyhow ! ( "Number out of range" ) ) ;
265+ }
266+
259267 encode:: write_u64 ( w, MajorKind :: UnsignedInt , i as u64 ) ?
260268 }
261269
You can’t perform that action at this time.
0 commit comments