Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binary-option: Fixed three integer overflows #3121

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions binary-option/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum BinaryOptionError {
PublicKeysShouldBeUnique,
#[error("TradePricesIncorrect")]
TradePricesIncorrect,
#[error("AmountOverflow")]
AmountOverflow,
}

impl From<BinaryOptionError> for ProgramError {
Expand Down
12 changes: 9 additions & 3 deletions binary-option/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ pub fn process_trade(
];

// Validate data
if buy_price + sell_price != u64::pow(10, binary_option.decimals as u32) {
let total_price = buy_price
.checked_add(sell_price)
.ok_or(BinaryOptionError::TradePricesIncorrect)?;
if total_price != u64::pow(10, binary_option.decimals as u32) {
return Err(BinaryOptionError::TradePricesIncorrect.into());
}
if binary_option.settled {
Expand Down Expand Up @@ -411,7 +414,7 @@ pub fn process_trade(
seeds,
)?;
if n > n_b + n_s {
binary_option.increment_supply(n - n_b - n_s);
binary_option.increment_supply(n - n_b - n_s)?;
} else {
binary_option.decrement_supply(n - n_b - n_s)?;
}
Expand Down Expand Up @@ -707,7 +710,10 @@ pub fn process_collect(program_id: &Pubkey, accounts: &[AccountInfo]) -> Program
seeds,
)?;
if reward > 0 {
let amount = (reward * escrow_account.amount) / binary_option.circulation;
let amount = reward
.checked_mul(escrow_account.amount)
.ok_or(BinaryOptionError::AmountOverflow)?;
let amount = amount / binary_option.circulation;
spl_token_transfer_signed(
token_program_info,
escrow_account_info,
Expand Down
8 changes: 6 additions & 2 deletions binary-option/program/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ impl BinaryOption {
Ok(binary_option)
}

pub fn increment_supply(&mut self, n: u64) {
self.circulation += n;
pub fn increment_supply(&mut self, n: u64) -> ProgramResult {
self.circulation = self
.circulation
.checked_add(n)
.ok_or(BinaryOptionError::AmountOverflow)?;
Ok(())
}

pub fn decrement_supply(&mut self, n: u64) -> ProgramResult {
Expand Down