Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type conversion related to as_u* #2847

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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