Skip to content

Commit 056ba1e

Browse files
author
Rafał Chabowski
committed
Use saturating_abs() to protect against overflow
1 parent 5c0f1fd commit 056ba1e

File tree

2 files changed

+2
-26
lines changed

2 files changed

+2
-26
lines changed

crates/fuel-gas-price-algorithm/src/utils.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,3 @@ pub(crate) fn cumulative_percentage_change(
1818
// `f64` over `u64::MAX` are cast to `u64::MAX`
1919
approx.ceil() as u64
2020
}
21-
22-
pub(crate) fn safe_signed_abs(n: i128) -> i128 {
23-
let n = if n == i128::MIN {
24-
n.saturating_add(1)
25-
} else {
26-
n
27-
};
28-
debug_assert!(n != i128::MIN);
29-
n.abs()
30-
}
31-
32-
#[cfg(test)]
33-
mod tests {
34-
use crate::utils::safe_signed_abs;
35-
36-
#[test]
37-
fn safe_signed_abs_does_not_overflow_on_min_value() {
38-
let abs = safe_signed_abs(i128::MIN);
39-
assert_eq!(abs, i128::MAX);
40-
}
41-
}

crates/fuel-gas-price-algorithm/src/v1.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use std::{
44
ops::Div,
55
};
66

7-
use crate::utils::{
8-
cumulative_percentage_change,
9-
safe_signed_abs,
10-
};
7+
use crate::utils::cumulative_percentage_change;
118

129
#[cfg(test)]
1310
mod tests;
@@ -334,7 +331,7 @@ impl AlgorithmUpdaterV1 {
334331
.saturating_mul(upcast_percent)
335332
.saturating_div(100)
336333
.into();
337-
let clamped_change = safe_signed_abs(pd_change).min(max_change);
334+
let clamped_change = pd_change.saturating_abs().min(max_change);
338335
pd_change.signum().saturating_mul(clamped_change)
339336
}
340337

0 commit comments

Comments
 (0)