Skip to content

Commit

Permalink
runtime-sdk/modules/evm: Make custom ABI-encoded error decoding easier
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Jul 27, 2023
1 parent 9104b4b commit de10c45
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
16 changes: 3 additions & 13 deletions runtime-sdk/modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,26 +232,16 @@ fn process_evm_result(exit_reason: evm::ExitReason, data: Vec<u8>) -> Result<Vec
const MIN_SIZE: usize = FIELD_REASON_START;
const MAX_REASON_SIZE: usize = 1024;

let max_raw_len = if data.len() > MAX_REASON_SIZE {
MAX_REASON_SIZE
} else {
data.len()
};
let max_raw_len = data.len().clamp(0, MAX_REASON_SIZE);
if data.len() < MIN_SIZE || !data.starts_with(ERROR_STRING_SELECTOR) {
return Err(Error::Reverted(format!(
"invalid reason prefix: '{}'",
base64::encode(&data[..max_raw_len])
)));
return Err(Error::Reverted(base64::encode(&data[..max_raw_len])));
}
// Decode and validate length.
let mut length =
primitive_types::U256::from(&data[FIELD_LENGTH_START..FIELD_LENGTH_START + 32])
.low_u32() as usize;
if FIELD_REASON_START + length > data.len() {
return Err(Error::Reverted(format!(
"invalid reason length: '{}'",
base64::encode(&data[..max_raw_len])
)));
return Err(Error::Reverted(base64::encode(&data[..max_raw_len])));
}
// Make sure that this doesn't ever return huge reason values as this is at least
// somewhat contract-controlled.
Expand Down
6 changes: 3 additions & 3 deletions runtime-sdk/modules/evm/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,23 +837,23 @@ fn test_revert_reason_decoding() {
// Malformed output, incorrect selector and bad length.
(
"BADBADBADBADBADBAD",
"invalid reason prefix: 'utututututut'",
"utututututut",
),
// Malformed output, bad selector.
(
"BAAAAAAD\
0000000000000000000000000000000000000000000000000000000000000020\
0000000000000000000000000000000000000000000000000000000000000018\
4461692f696e73756666696369656e742d62616c616e63650000000000000000",
"invalid reason prefix: 'uqqqrQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABhEYWkvaW5zdWZmaWNpZW50LWJhbGFuY2UAAAAAAAAAAA=='",
"uqqqrQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABhEYWkvaW5zdWZmaWNpZW50LWJhbGFuY2UAAAAAAAAAAA==",
),
// Malformed output, corrupted length.
(
"08c379a0\
0000000000000000000000000000000000000000000000000000000000000020\
00000000000000000000000000000000000000000000000000000000FFFFFFFF\
4461692f696e73756666696369656e742d62616c616e63650000000000000000",
"invalid reason length: 'CMN5oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP////9EYWkvaW5zdWZmaWNpZW50LWJhbGFuY2UAAAAAAAAAAA=='",
"CMN5oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP////9EYWkvaW5zdWZmaWNpZW50LWJhbGFuY2UAAAAAAAAAAA==",
),
];

Expand Down

0 comments on commit de10c45

Please sign in to comment.