@@ -566,9 +566,24 @@ where
566566 Ok ( Value :: from ( d) )
567567 }
568568 fn read_decimal_data ( & mut self ) -> Result < Value , ReadError > {
569- let mantisa = self . read_int_data ( ) ?;
569+ let mantissa = self . read_int_data ( ) ?;
570570 let exponent = self . read_int_data ( ) ?;
571- let d = Decimal :: new ( mantisa, exponent as i8 ) ;
571+
572+ if exponent < i64:: from ( i8:: MIN ) || exponent > i64:: from ( i8:: MAX ) {
573+ return Err ( self . make_error (
574+ & format ! ( "Decimal exponent {exponent} overflows i8" ) ,
575+ ReadErrorReason :: NumericValueOverflow ,
576+ ) ) ;
577+ }
578+
579+ if !( -( 1i64 << 55 ) ..( 1i64 << 55 ) ) . contains ( & mantissa) {
580+ return Err ( self . make_error (
581+ & format ! ( "Decimal mantissa {mantissa} overflows 56-bit field" ) ,
582+ ReadErrorReason :: NumericValueOverflow ,
583+ ) ) ;
584+ }
585+
586+ let d = Decimal :: new ( mantissa, exponent as i8 ) ;
572587 Ok ( Value :: from ( d) )
573588 }
574589
@@ -884,6 +899,8 @@ fn test_decimal() {
884899 let dec = crate :: decimal:: Decimal :: new ( 0 , 0 ) ;
885900 assert_eq ! ( hex_chainpack_to_rpcvalue( "8C0000" ) . unwrap( ) , dec. into( ) ) ;
886901 assert_eq ! ( rpcvalue_to_hex_chainpack( & dec. into( ) ) , "8C0000" ) ;
902+ assert_eq ! ( hex_chainpack_to_rpcvalue( "8CF400B201AB082063B54F" ) . unwrap_err( ) . msg, "ChainPack read error - Decimal mantissa 50104379941872565 overflows 56-bit field" ) ;
903+ assert_eq ! ( hex_chainpack_to_rpcvalue( "8C4FF400B201AB082063B5" ) . unwrap_err( ) . msg, "ChainPack read error - Decimal exponent 50104379941872565 overflows i8" ) ;
887904}
888905
889906#[ test]
0 commit comments