Skip to content

Commit 1efc819

Browse files
committed
chainpack: Handle Decimal overflows
1 parent d485fa7 commit 1efc819

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name = "shvproto"
55
description = "Rust implementation of the SHV protocol"
66
license = "MIT"
77
repository = "https://github.com/silicon-heaven/libshvproto-rs"
8-
version = "6.1.7"
8+
version = "6.1.8"
99
edition = "2024"
1010

1111
[dependencies]

src/chainpack.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)