From 0856f6ccf256b55650276fd40649807d5cf849a8 Mon Sep 17 00:00:00 2001 From: alessandrokonrad Date: Mon, 17 Oct 2022 12:07:12 +0200 Subject: [PATCH] added checked operators to some DefaultFunction --- crates/uplc/src/machine/error.rs | 2 ++ crates/uplc/src/machine/runtime.rs | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/crates/uplc/src/machine/error.rs b/crates/uplc/src/machine/error.rs index ba81ab79..d9de5344 100644 --- a/crates/uplc/src/machine/error.rs +++ b/crates/uplc/src/machine/error.rs @@ -46,4 +46,6 @@ pub enum Error { UnexpectedEd25519SignatureLength(usize), #[error("Failed to deserialise PlutusData:\n\n{0:#?}")] DeserialisationError(Value), + #[error("Integer overflow")] + OverflowError, } diff --git a/crates/uplc/src/machine/runtime.rs b/crates/uplc/src/machine/runtime.rs index 5cca10c2..cd93ea1e 100644 --- a/crates/uplc/src/machine/runtime.rs +++ b/crates/uplc/src/machine/runtime.rs @@ -328,19 +328,28 @@ impl DefaultFunction { match self { DefaultFunction::AddInteger => match (&args[0], &args[1]) { (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!(), }, DefaultFunction::SubtractInteger => match (&args[0], &args[1]) { (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!(), }, DefaultFunction::MultiplyInteger => match (&args[0], &args[1]) { (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!(), },