From d3eff6ddbc1f1718e662d65c756889621288fc50 Mon Sep 17 00:00:00 2001 From: Iulian Barbu <14218860+iulianbarbu@users.noreply.github.com> Date: Fri, 11 Apr 2025 20:06:48 +0300 Subject: [PATCH] Use `TryFrom` impls instead of `as` operator in `polkadot-runtime-parachains` (#8118) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description - changed conversions based on `as` operator to `usize::try_from` in `polkadot-runtime-parachains` ## Integration N/A ## Review Notes N/A --------- Signed-off-by: Iulian Barbu Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Bastian Köcher Co-authored-by: Bastian Köcher Co-authored-by: teor Co-authored-by: Guillaume Thiolliere (cherry picked from commit fc0fcbaecab7dc7e0c3a0ff380c8a81af8b7c48b) --- polkadot/runtime/parachains/src/inclusion/mod.rs | 14 +++++++++----- prdoc/pr_8118.prdoc | 8 ++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 prdoc/pr_8118.prdoc diff --git a/polkadot/runtime/parachains/src/inclusion/mod.rs b/polkadot/runtime/parachains/src/inclusion/mod.rs index 8ad9711a0f388..387a55831550c 100644 --- a/polkadot/runtime/parachains/src/inclusion/mod.rs +++ b/polkadot/runtime/parachains/src/inclusion/mod.rs @@ -1306,19 +1306,23 @@ impl CandidateCheckContext { hrmp_watermark: BlockNumberFor, horizontal_messages: &[polkadot_primitives::OutboundHrmpMessage], ) -> Result<(), AcceptanceCheckErr> { - ensure!( - head_data.0.len() <= self.config.max_head_data_size as _, - AcceptanceCheckErr::HeadDataTooLarge, - ); + // Safe convertions when `self.config.max_head_data_size` is in bounds of `usize` type. + let max_head_data_size = usize::try_from(self.config.max_head_data_size) + .map_err(|_| AcceptanceCheckErr::HeadDataTooLarge)?; + ensure!(head_data.0.len() <= max_head_data_size, AcceptanceCheckErr::HeadDataTooLarge); // if any, the code upgrade attempt is allowed. if let Some(new_validation_code) = new_validation_code { + // Safe convertions when `self.config.max_code_size` is in bounds of `usize` type. + let max_code_size = usize::try_from(self.config.max_code_size) + .map_err(|_| AcceptanceCheckErr::NewCodeTooLarge)?; + ensure!( paras::Pallet::::can_upgrade_validation_code(para_id), AcceptanceCheckErr::PrematureCodeUpgrade, ); ensure!( - new_validation_code.0.len() <= self.config.max_code_size as _, + new_validation_code.0.len() <= max_code_size, AcceptanceCheckErr::NewCodeTooLarge, ); } diff --git a/prdoc/pr_8118.prdoc b/prdoc/pr_8118.prdoc new file mode 100644 index 0000000000000..ae997635d7da1 --- /dev/null +++ b/prdoc/pr_8118.prdoc @@ -0,0 +1,8 @@ +title: Safer conversions in polkadot-runtime-parachains +doc: +- audience: Runtime Dev + description: |- + Use TryFrom impls instead of `as` operator for conversions. +crates: +- name: polkadot-runtime-parachains + bump: patch