Skip to content

Commit

Permalink
Fix type conversion related to as_u* (#2847)
Browse files Browse the repository at this point in the history
* fix type conversion related to as_u*

* fix clippy
  • Loading branch information
zjb0807 authored Jan 14, 2025
1 parent c61d548 commit b5df677
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
12 changes: 8 additions & 4 deletions modules/evm-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,20 @@ impl<T: Config> Pallet<T> {
Error::<T>::InvalidReturnValue
);

let offset = U256::from_big_endian(&output[0..32]);
let length = U256::from_big_endian(&output[offset.as_usize()..offset.as_usize() + 32]);
let offset = U256::from_big_endian(output.get(0..32).unwrap_or_default());
let offset: usize = offset.try_into().map_err(|_| Error::<T>::InvalidReturnValue)?;
let offset_end = offset.checked_add(32).ok_or(Error::<T>::InvalidReturnValue)?;
let length = U256::from_big_endian(output.get(offset..offset_end).unwrap_or_default());
let length: usize = length.try_into().map_err(|_| Error::<T>::InvalidReturnValue)?;
let length_end = offset_end.checked_add(length).ok_or(Error::<T>::InvalidReturnValue)?;
ensure!(
// output is 32-byte aligned. ensure total_length >= offset + string length + string data length.
output.len() >= offset.as_usize() + 32 + length.as_usize(),
output.len() >= length_end,
Error::<T>::InvalidReturnValue
);

let mut data = Vec::new();
data.extend_from_slice(&output[offset.as_usize() + 32..offset.as_usize() + 32 + length.as_usize()]);
data.extend_from_slice(output.get(offset_end..length_end).unwrap_or_default());

Ok(data.to_vec())
}
Expand Down
8 changes: 6 additions & 2 deletions modules/evm/src/precompiles/modexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,20 @@ impl Precompile for IstanbulModexp {
});
}
let cost = ModexpPricer::cost(Self::DIVISOR, input);
let cost = TryInto::<u64>::try_into(cost).map_err(|_| PrecompileFailure::Error {
exit_status: ExitError::OutOfGas,
})?;

if let Some(target_gas) = target_gas {
if cost > U256::from(u64::MAX) || target_gas < cost.as_u64() {
if target_gas < cost {
return Err(PrecompileFailure::Error {
exit_status: ExitError::OutOfGas,
});
}
}

let output = Self::execute_modexp(input);
handle.record_cost(cost.as_u64())?;
handle.record_cost(cost)?;

Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
Expand Down
8 changes: 4 additions & 4 deletions modules/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,11 @@ impl<'vicinity, 'config, T: Config> BackendT for SubstrateStackState<'vicinity,
}

fn block_hash(&self, number: U256) -> H256 {
if number > U256::from(u32::MAX) {
H256::default()
} else {
let number = BlockNumberFor::<T>::from(number.as_u32());
if let Ok(number) = TryInto::<u32>::try_into(number) {
let number = BlockNumberFor::<T>::from(number);
H256::from_slice(frame_system::Pallet::<T>::block_hash(number).as_ref())
} else {
H256::default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion orml

0 comments on commit b5df677

Please sign in to comment.