Skip to content

Commit 231cbaa

Browse files
committed
address comment
1 parent 83a0e74 commit 231cbaa

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

crates/shared/src/bad_token/trace_call.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,23 +281,23 @@ impl TraceCallDetectorRaw {
281281
let bad = TokenQuality::Bad {
282282
reason: message.to_string(),
283283
};
284-
let balance_before_in = match U256::try_from_be_slice(&traces[0].output) {
284+
let balance_before_in = match u256_from_be_bytes_strict(&traces[0].output) {
285285
Some(balance) => balance,
286286
None => return Ok(bad),
287287
};
288-
let balance_after_in = match U256::try_from_be_slice(&traces[2].output) {
288+
let balance_after_in = match u256_from_be_bytes_strict(&traces[2].output) {
289289
Some(balance) => balance,
290290
None => return Ok(bad),
291291
};
292-
let balance_after_out = match U256::try_from_be_slice(&traces[5].output) {
292+
let balance_after_out = match u256_from_be_bytes_strict(&traces[5].output) {
293293
Some(balance) => balance,
294294
None => return Ok(bad),
295295
};
296-
let balance_recipient_before = match U256::try_from_be_slice(&traces[3].output) {
296+
let balance_recipient_before = match u256_from_be_bytes_strict(&traces[3].output) {
297297
Some(balance) => balance,
298298
None => return Ok(bad),
299299
};
300-
let balance_recipient_after = match U256::try_from_be_slice(&traces[6].output) {
300+
let balance_recipient_after = match u256_from_be_bytes_strict(&traces[6].output) {
301301
Some(balance) => balance,
302302
None => return Ok(bad),
303303
};
@@ -376,6 +376,15 @@ fn ensure_transaction_ok_and_get_gas(trace: &TraceResults) -> Result<Result<U256
376376
Ok(Ok(U256::from(call_result.gas_used)))
377377
}
378378

379+
/// Decodes a `U256` from a big-endian encoded slice.
380+
/// The slice's length MUST be 32 bytes.
381+
fn u256_from_be_bytes_strict(b: &[u8]) -> Option<U256> {
382+
if b.len() != 32 {
383+
return None;
384+
}
385+
U256::try_from_be_slice(b)
386+
}
387+
379388
#[cfg(test)]
380389
mod tests {
381390
use {

0 commit comments

Comments
 (0)