Skip to content

Commit f2cc3e7

Browse files
authored
Optimize gas cost subtraction by using checked_sub for safer arithmetic bluealloy#1884
1 parent a492073 commit f2cc3e7

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

crates/interpreter/src/gas.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ impl Gas {
136136
/// Returns `false` if the gas limit is exceeded.
137137
#[inline]
138138
#[must_use = "prefer using `gas!` instead to return an out-of-gas error on failure"]
139-
pub fn record_cost(&mut self, cost: u64) -> bool {
140-
let (remaining, overflow) = self.remaining.overflowing_sub(cost);
141-
let success = !overflow;
142-
if success {
143-
self.remaining = remaining;
139+
pub fn record_cost(&mut self, cost: u64) -> bool {
140+
if let Some(new_remaining) = self.remaining.checked_sub(cost) {
141+
self.remaining = new_remaining;
142+
true
143+
} else {
144+
false // OO Gas
144145
}
145-
success
146146
}
147147

148148
/// Record memory expansion

0 commit comments

Comments
 (0)