@@ -440,6 +440,14 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
440
440
if !Self :: is_hex_string ( string) {
441
441
return Err ( CreationError :: InvalidHexString ) ;
442
442
}
443
+
444
+ // Limbs are of 64 bits - 8 bytes
445
+ // We have 16 nibbles per bytes
446
+ let max_amount_of_hex_chars = NUM_LIMBS * 16 ;
447
+ if string. len ( ) > max_amount_of_hex_chars {
448
+ return Err ( CreationError :: HexStringIsTooBig ) ;
449
+ }
450
+
443
451
Ok ( Self :: from_hex_unchecked ( string) )
444
452
}
445
453
@@ -1001,7 +1009,7 @@ impl<const NUM_LIMBS: usize> Arbitrary for UnsignedInteger<NUM_LIMBS> {
1001
1009
#[ cfg( test) ]
1002
1010
mod tests_u384 {
1003
1011
use crate :: traits:: ByteConversion ;
1004
- use crate :: unsigned_integer:: element:: { UnsignedInteger , U384 } ;
1012
+ use crate :: unsigned_integer:: element:: { UnsignedInteger , U256 , U384 } ;
1005
1013
#[ cfg( feature = "proptest" ) ]
1006
1014
use proptest:: prelude:: * ;
1007
1015
#[ cfg( feature = "proptest" ) ]
@@ -1246,6 +1254,18 @@ mod tests_u384 {
1246
1254
) ;
1247
1255
}
1248
1256
1257
+ #[ test]
1258
+ fn from_hex_with_overflowing_hexstring_should_error ( ) {
1259
+ let u256_from_big_string = U256 :: from_hex ( & "f" . repeat ( 65 ) ) ;
1260
+ assert ! ( u256_from_big_string. is_err( ) ) ;
1261
+ assert ! ( u256_from_big_string == Err ( crate :: unsigned_integer:: element:: CreationError :: HexStringIsTooBig ) ) ;
1262
+ }
1263
+
1264
+ #[ test]
1265
+ fn from_hex_with_non_overflowing_hexstring_should_work ( ) {
1266
+ assert_eq ! ( U256 :: from_hex( & "0" . repeat( 64 ) ) . unwrap( ) . limbs, [ 0 , 0 , 0 , 0 ] )
1267
+ }
1268
+
1249
1269
#[ test]
1250
1270
fn construct_new_integer_from_dec_1 ( ) {
1251
1271
let a = U384 :: from_dec_str ( "1" ) . unwrap ( ) ;
0 commit comments