mirror of
https://github.com/jwinterm/grin-wallet.git
synced 2026-11-10 17:53:53 +01:00
Contracts: use checked conversions for amount -> i64
A large u64 amount could wrap when cast `as i64`. Convert with i64::try_from in the CLI net-change derivation and in contract view, returning an error instead. get_net_change / to_api_args now return Result (the two arg panics become errors too), and the to_api_args call moves out of the owner-session closure. Addresses PR #754 review (command.rs, view.rs).
This commit is contained in:
@@ -37,10 +37,14 @@ where
|
||||
// Otherwise, you can't really predict who the party doing the next step should be.
|
||||
|
||||
// TODO: Do we need to do any slate verification here?
|
||||
// Checked conversion so an out-of-range amount can't wrap into a bogus net change.
|
||||
let suggested_net_change: Option<i64> = match slate.state {
|
||||
// TODO: Check bounds against overflow/underflow
|
||||
SlateState::Invoice1 => Some(slate.amount as i64),
|
||||
SlateState::Standard1 => Some(-(slate.amount as i64)),
|
||||
SlateState::Invoice1 => Some(i64::try_from(slate.amount).map_err(|_| {
|
||||
Error::GenericError(format!("Slate amount {} exceeds i64", slate.amount))
|
||||
})?),
|
||||
SlateState::Standard1 => Some(-i64::try_from(slate.amount).map_err(|_| {
|
||||
Error::GenericError(format!("Slate amount {} exceeds i64", slate.amount))
|
||||
})?),
|
||||
_ => None,
|
||||
};
|
||||
let is_executed = false;
|
||||
|
||||
Reference in New Issue
Block a user