Skip to content

Commit

Permalink
overflowing to checked where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Aug 7, 2024
1 parent 2a4f6b8 commit e8546cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
35 changes: 16 additions & 19 deletions packages/bytes/src/storage.cairo
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
Expand Down
21 changes: 3 additions & 18 deletions packages/data_structures/src/span_ext.cairo
Original file line number Diff line number Diff line change
@@ -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<T> {
Expand Down Expand Up @@ -52,14 +51,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
fn pop_back_n(ref self: Span<T>, n: usize) -> Span<T> {
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);
Expand All @@ -77,14 +69,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
fn remove_back_n(ref self: Span<T>, 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);
}
Expand Down

0 comments on commit e8546cf

Please sign in to comment.