1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0
/// Utility macro for writing secure arithmetic operations in order to avoid
/// integer overflows.
///
/// # Examples
///
/// ```
///# use crate::diem_infallible::checked;
/// let a: i64 = 1;
/// let b: i64 = 2;
/// let c: i64 = 3;
///
/// assert_eq!(checked!(a + b).unwrap(), 3);
/// assert_eq!(checked!(a + b + c).unwrap(), 6);
///
/// // When doing multiple different operations, it's important to use parentheses in order
/// // to guarantee the order of operation!
/// assert_eq!(checked!(a + ((b - c) * c)).unwrap(), -2);
///
/// // When using numeric literals, the compiler might not be able to infer the type properly,
/// // so if it complains, just add the type to the number.
/// assert_eq!(checked!(10_u32 / 2_u32).unwrap(), 5);
/// assert_eq!(checked!(10_u32 * 2_u32).unwrap(), 20);
/// assert_eq!(checked!(10_u32 - 2_u32).unwrap(), 8);
/// assert_eq!(checked!(2_i32 - 10_i32).unwrap(), -8);
/// assert_eq!(checked!(10_u32 + 2_u32).unwrap(), 12);
///
/// // Casts using `as` operator must appear within parenthesis
/// assert_eq!(checked!(10_u32 + (2_u16 as u32)).unwrap(), 12);
///
/// assert_eq!(checked!(10_u32 / (1_u32 + 1_u32)).unwrap(), 5);
/// assert_eq!(checked!(10_u32 * (1_u32 + 1_u32)).unwrap(), 20);
/// assert_eq!(checked!(10_u32 - (1_u32 + 1_u32)).unwrap(), 8);
/// assert_eq!(checked!(10_u32 + (1_u32 + 1_u32)).unwrap(), 12);
///
/// let max = u32::max_value();
/// assert!(checked!(max + 1_u32).is_err());
/// assert!(checked!(0_u32 - 1_u32).is_err());
///
/// # struct Foo {
/// # pub bar: i32
/// # }
/// # impl Foo {
/// # pub fn one() -> i32 {
/// # 1
/// # }
/// # }
/// // When one of the operands is an associated function or member, due to limitations with the
/// // macro syntax which disallows an `expr` to precede a `+` sign, make sure to wrap the expression
/// // in parenthesis
/// # let foo = Foo { bar: 1 };
/// assert_eq!(checked!((foo.bar) + 1_i32).unwrap(), 2);
/// assert_eq!(checked!(1_i32 + (Foo::one())).unwrap(), 2);
/// ```
#[macro_export]
macro_rules! checked {
($a:tt + $b:tt) => {{
$a.checked_add($b).ok_or_else(|| $crate::ArithmeticError(format!("Operation results in overflow/underflow: {} + {}", $a, $b)))
}};
($a:tt - $b:tt) => {{
$a.checked_sub($b).ok_or_else(|| $crate::ArithmeticError(format!("Operation results in overflow/underflow: {} - {}", $a, $b)))
}};
($a:tt * $b:tt) => {{
$a.checked_mul($b).ok_or_else(|| $crate::ArithmeticError(format!("Operation results in overflow/underflow: {} * {}", $a, $b)))
}};
($a:tt / $b:tt) => {{
$a.checked_div($b).ok_or_else(|| $crate::ArithmeticError(format!("Operation results in overflow/underflow: {} / {}", $a, $b)))
}};
($a:tt + $($tokens:tt)*) => {{
checked!( $($tokens)* ).and_then(|b| {
b.checked_add($a)
.ok_or_else(|| $crate::ArithmeticError(format!("Operation results in overflow/underflow: {} + {}", b, $a)))
})
}};
($a:tt - $($tokens:tt)*) => {{
checked!( $($tokens)* ).and_then(|b| {
b.checked_sub($a)
.ok_or_else(|| $crate::ArithmeticError(format!("Operation results in overflow/underflow: {} - {}", b, $a)))
})
}};
($a:tt * $($tokens:tt)*) => {{
checked!( $($tokens)* ).and_then(|b| {
b.checked_mul($a)
.ok_or_else(|| $crate::ArithmeticError(format!("Operation results in overflow/underflow: {} * {}", b, $a)))
})
}};
($a:tt / $($tokens:tt)*) => {{
checked!( $($tokens)* ).and_then(|b| {
b.checked_div($a)
.ok_or_else(|| $crate::ArithmeticError(format!("Operation results in overflow/underflow: {} / {}", b, $a)))
})
}};
}
#[derive(Debug)]
pub struct ArithmeticError(pub String);
impl std::fmt::Display for ArithmeticError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl std::error::Error for ArithmeticError {
fn description(&self) -> &str {
&self.0
}
}