Remove potentially-failing unchecked arithmetic operations for ones which error

In response to 9.13.3.

Requires a bump to Rust 1.82 to take advantage of `Option::is_none_or`.
This commit is contained in:
Luke Parker
2025-08-08 22:30:34 -04:00
parent cbab9486c6
commit 188fcc3cb4
13 changed files with 65 additions and 18 deletions

View File

@@ -6,7 +6,7 @@ license = "MIT"
repository = "https://github.com/serai-dex/serai/tree/develop/networks/monero/wallet"
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
edition = "2021"
rust-version = "1.80"
rust-version = "1.82"
[package.metadata.docs.rs]
all-features = true

View File

@@ -94,11 +94,10 @@ pub(crate) fn encode_check(mut data: Vec<u8>) -> String {
// Decode an arbitrary-length stream of data, with a checksum
pub(crate) fn decode_check(data: &str) -> Option<Vec<u8>> {
if data.len() < CHECKSUM_LEN {
let mut res = decode(data)?;
if res.len() < CHECKSUM_LEN {
None?;
}
let mut res = decode(data)?;
let checksum_pos = res.len() - CHECKSUM_LEN;
if keccak256(&res[.. checksum_pos])[.. CHECKSUM_LEN] != res[checksum_pos ..] {
None?;

View File

@@ -18,7 +18,7 @@ use crate::{
};
const RECENT_WINDOW: u64 = 15;
const BLOCKS_PER_YEAR: usize = 365 * 24 * 60 * 60 / BLOCK_TIME;
const BLOCKS_PER_YEAR: usize = (365 * 24 * 60 * 60) / BLOCK_TIME;
#[allow(clippy::cast_precision_loss)]
const TIP_APPLICATION: f64 = (DEFAULT_LOCK_WINDOW * BLOCK_TIME) as f64;

View File

@@ -232,7 +232,13 @@ impl InternalScanner {
res.push(WalletOutput {
absolute_id: AbsoluteId { transaction: tx_hash, index_in_transaction: o },
relative_id: RelativeId { index_on_blockchain: output_index_for_first_ringct_output + o },
relative_id: RelativeId {
index_on_blockchain: output_index_for_first_ringct_output.checked_add(o).ok_or(
ScanError::InvalidScannableBlock(
"transaction's output's index isn't representable as a u64",
),
)?,
},
data: OutputData { key: output_key, key_offset, commitment },
metadata: Metadata {
additional_timelock: tx.prefix().additional_timelock,

View File

@@ -305,12 +305,13 @@ impl SignableTransaction {
.payments
.iter()
.filter_map(|payment| match payment {
InternalPayment::Payment(_, amount) => Some(amount),
InternalPayment::Payment(_, amount) => Some(*amount),
InternalPayment::Change(_) => None,
})
.sum::<u64>();
.try_fold(0, u64::checked_add);
let payments_amount = payments_amount.ok_or(SendError::TooManyOutputs)?;
let (weight, necessary_fee) = self.weight_and_necessary_fee();
if in_amount < (payments_amount + necessary_fee) {
if payments_amount.checked_add(necessary_fee).is_none_or(|total_out| in_amount < total_out) {
Err(SendError::NotEnoughFunds {
inputs: in_amount,
outputs: payments_amount,