added checked operators to some DefaultFunction

This commit is contained in:
alessandrokonrad 2022-10-17 12:07:12 +02:00 committed by Lucas
parent cfc1f92646
commit 0856f6ccf2
2 changed files with 14 additions and 3 deletions

View File

@ -46,4 +46,6 @@ pub enum Error {
UnexpectedEd25519SignatureLength(usize), UnexpectedEd25519SignatureLength(usize),
#[error("Failed to deserialise PlutusData:\n\n{0:#?}")] #[error("Failed to deserialise PlutusData:\n\n{0:#?}")]
DeserialisationError(Value), DeserialisationError(Value),
#[error("Integer overflow")]
OverflowError,
} }

View File

@ -328,19 +328,28 @@ impl DefaultFunction {
match self { match self {
DefaultFunction::AddInteger => match (&args[0], &args[1]) { DefaultFunction::AddInteger => match (&args[0], &args[1]) {
(Value::Con(Constant::Integer(arg1)), Value::Con(Constant::Integer(arg2))) => { (Value::Con(Constant::Integer(arg1)), Value::Con(Constant::Integer(arg2))) => {
Ok(Value::Con(Constant::Integer(arg1 + arg2))) match arg1.checked_add(*arg2) {
Some(res) => Ok(Value::Con(Constant::Integer(res))),
None => return Err(Error::OverflowError),
}
} }
_ => unreachable!(), _ => unreachable!(),
}, },
DefaultFunction::SubtractInteger => match (&args[0], &args[1]) { DefaultFunction::SubtractInteger => match (&args[0], &args[1]) {
(Value::Con(Constant::Integer(arg1)), Value::Con(Constant::Integer(arg2))) => { (Value::Con(Constant::Integer(arg1)), Value::Con(Constant::Integer(arg2))) => {
Ok(Value::Con(Constant::Integer(arg1 - arg2))) match arg1.checked_sub(*arg2) {
Some(res) => Ok(Value::Con(Constant::Integer(res))),
None => return Err(Error::OverflowError),
}
} }
_ => unreachable!(), _ => unreachable!(),
}, },
DefaultFunction::MultiplyInteger => match (&args[0], &args[1]) { DefaultFunction::MultiplyInteger => match (&args[0], &args[1]) {
(Value::Con(Constant::Integer(arg1)), Value::Con(Constant::Integer(arg2))) => { (Value::Con(Constant::Integer(arg1)), Value::Con(Constant::Integer(arg2))) => {
Ok(Value::Con(Constant::Integer(arg1 * arg2))) match arg1.checked_mul(*arg2) {
Some(res) => Ok(Value::Con(Constant::Integer(res))),
None => return Err(Error::OverflowError),
}
} }
_ => unreachable!(), _ => unreachable!(),
}, },