Skip to content

Commit 8d63d54

Browse files
authored
Fix length checking overflow in EVM revert reason parsing (polkadot-evm#820)
* Fix length checking overflow in EVM revert reason parsing * Remove unused debug_assert
1 parent 969b364 commit 8d63d54

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

client/rpc/src/eth/execute.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use sp_blockchain::{BlockStatus, HeaderBackend};
3131
use sp_runtime::{
3232
generic::BlockId,
3333
traits::{BlakeTwo256, Block as BlockT},
34+
SaturatedConversion,
3435
};
3536

3637
use fc_rpc_core::types::*;
@@ -688,13 +689,19 @@ pub fn error_on_execution_failure(reason: &ExitReason, data: &[u8]) -> Result<()
688689
))
689690
}
690691
ExitReason::Revert(_) => {
692+
const LEN_START: usize = 36;
693+
const MESSAGE_START: usize = 68;
694+
691695
let mut message = "VM Exception while processing transaction: revert".to_string();
692696
// A minimum size of error function selector (4) + offset (32) + string length (32)
693697
// should contain a utf-8 encoded revert reason.
694-
if data.len() > 68 {
695-
let message_len = data[36..68].iter().sum::<u8>();
696-
if data.len() >= 68 + message_len as usize {
697-
let body: &[u8] = &data[68..68 + message_len as usize];
698+
if data.len() > MESSAGE_START {
699+
let message_len =
700+
U256::from(&data[LEN_START..MESSAGE_START]).saturated_into::<usize>();
701+
let message_end = MESSAGE_START.saturating_add(message_len);
702+
703+
if data.len() >= message_end {
704+
let body: &[u8] = &data[MESSAGE_START..message_end];
698705
if let Ok(reason) = std::str::from_utf8(body) {
699706
message = format!("{} {}", message, reason);
700707
}

0 commit comments

Comments
 (0)