From e8546cff07a4d252e3669bf37407669f838a150e Mon Sep 17 00:00:00 2001 From: gaetbout Date: Wed, 7 Aug 2024 15:37:49 +0200 Subject: [PATCH] overflowing to checked where possible --- packages/bytes/src/storage.cairo | 35 ++++++++++----------- packages/data_structures/src/span_ext.cairo | 21 ++----------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/packages/bytes/src/storage.cairo b/packages/bytes/src/storage.cairo index 94db2d31..25fb3801 100644 --- a/packages/bytes/src/storage.cairo +++ b/packages/bytes/src/storage.cairo @@ -1,5 +1,5 @@ use alexandria_bytes::bytes::{Bytes, BytesTrait, BYTES_PER_ELEMENT}; -use core::num::traits::OverflowingAdd; +use core::num::traits::CheckedAdd; use starknet::SyscallResult; use starknet::storage_access::{ Store, StorageAddress, StorageBaseAddress, storage_address_from_base, @@ -88,15 +88,13 @@ fn inner_read_bytes(address_domain: u32, address: StorageAddress) -> SyscallResu data.append(value); remaining_full_words -= 1; - let (tmp, did_overflow) = index_in_chunk.overflowing_add(1); - if did_overflow { - // After reading 256 `uint128`s `index_in_chunk` will overflow and we move to the - // next chunk. - chunk += 1; - chunk_base = inner_bytes_pointer(address, chunk); - index_in_chunk = 0; - } else { - index_in_chunk = tmp; + match index_in_chunk.checked_add(1) { + Option::Some(x) => { index_in_chunk = x; }, + Option::None => { + chunk += 1; + chunk_base = inner_bytes_pointer(address, chunk); + index_in_chunk = 0; + }, } }?; if last_word_len != 0 { @@ -133,15 +131,14 @@ fn inner_write_bytes( Result::Ok(_) => {}, Result::Err(err) => { break Result::Err(err); }, }; - let (tmp, did_overflow) = index_in_chunk.overflowing_add(1); - if did_overflow { - // After writing 256 `uint128`s `index_in_chunk` will overflow and we move to the - // next chunk. - chunk += 1; - chunk_base = inner_bytes_pointer(address, chunk); - index_in_chunk = 0; - } else { - index_in_chunk = tmp; + + match index_in_chunk.checked_add(1) { + Option::Some(x) => { index_in_chunk = x; }, + Option::None => { + chunk += 1; + chunk_base = inner_bytes_pointer(address, chunk); + index_in_chunk = 0; + }, } }?; Result::Ok(()) diff --git a/packages/data_structures/src/span_ext.cairo b/packages/data_structures/src/span_ext.cairo index 75e81c21..211b1ec5 100644 --- a/packages/data_structures/src/span_ext.cairo +++ b/packages/data_structures/src/span_ext.cairo @@ -1,7 +1,6 @@ use core::clone::Clone; use core::cmp::min; -use core::num::traits::OverflowingSub; -use core::option::OptionTrait; +use core::num::traits::CheckedSub; use super::array_ext::ArrayTraitExt; pub trait SpanTraitExt { @@ -52,14 +51,7 @@ impl SpanImpl, +Drop> of SpanTraitExt { fn pop_back_n(ref self: Span, n: usize) -> Span { let span_len = self.len(); // Saturating substraction - let separator = { - let (value, overflow) = span_len.overflowing_sub(n); - if overflow { - 0 - } else { - value - } - }; + let separator = span_len.checked_sub(n).unwrap_or(0); let res = self.slice(separator, span_len - separator); self = self.slice(0, separator); @@ -77,14 +69,7 @@ impl SpanImpl, +Drop> of SpanTraitExt { fn remove_back_n(ref self: Span, mut n: usize) { let span_len = self.len(); // Saturating substraction - let separator = { - let (value, overflow) = span_len.overflowing_sub(n); - if overflow { - 0 - } else { - value - } - }; + let separator = span_len.checked_sub(n).unwrap_or(0); self = self.slice(0, separator); }