Skip to content

Commit 4f6e098

Browse files
committed
Improve coption validation
1 parent e0514fd commit 4f6e098

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

interface/src/state/account.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
super::{account_state::AccountState, COption, Initializable, Transmutable},
2+
super::{account_state::AccountState, validate_option, COption, Initializable, Transmutable},
33
pinocchio::{program_error::ProgramError, pubkey::Pubkey},
44
};
55

@@ -159,24 +159,16 @@ impl Initializable for Account {
159159
#[inline(always)]
160160
fn is_initialized(&self) -> Result<bool, ProgramError> {
161161
// delegate
162-
match self.delegate.0 {
163-
[0, 0, 0, 0] | [1, 0, 0, 0] => (),
164-
_ => return Err(ProgramError::InvalidAccountData),
165-
}
162+
validate_option(self.delegate.0)?;
163+
166164
// state
167165
let state = AccountState::try_from(self.state)?;
168166

169167
// is_native
170-
match self.is_native {
171-
[0, 0, 0, 0] | [1, 0, 0, 0] => (),
172-
_ => return Err(ProgramError::InvalidAccountData),
173-
}
168+
validate_option(self.is_native)?;
174169

175170
// close authority
176-
match self.close_authority.0 {
177-
[0, 0, 0, 0] | [1, 0, 0, 0] => (),
178-
_ => return Err(ProgramError::InvalidAccountData),
179-
}
171+
validate_option(self.close_authority.0)?;
180172

181173
Ok(state != AccountState::Uninitialized)
182174
}

interface/src/state/mint.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
super::{COption, Initializable, Transmutable},
2+
super::{validate_option, COption, Initializable, Transmutable},
33
pinocchio::{program_error::ProgramError, pubkey::Pubkey},
44
};
55

@@ -93,10 +93,7 @@ impl Initializable for Mint {
9393
#[inline(always)]
9494
fn is_initialized(&self) -> Result<bool, ProgramError> {
9595
// mint_authority
96-
match self.mint_authority.0 {
97-
[0, 0, 0, 0] | [1, 0, 0, 0] => (),
98-
_ => return Err(ProgramError::InvalidAccountData),
99-
}
96+
validate_option(self.mint_authority.0)?;
10097

10198
// is_initialized
10299
let initialized = match self.is_initialized {
@@ -106,10 +103,7 @@ impl Initializable for Mint {
106103
};
107104

108105
// freeze_authority
109-
match self.freeze_authority.0 {
110-
[0, 0, 0, 0] | [1, 0, 0, 0] => (),
111-
_ => return Err(ProgramError::InvalidAccountData),
112-
}
106+
validate_option(self.freeze_authority.0)?;
113107

114108
Ok(initialized)
115109
}

interface/src/state/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use pinocchio::program_error::ProgramError;
1+
use pinocchio::{program_error::ProgramError, ProgramResult};
22

33
pub mod account;
44
pub mod account_state;
@@ -97,3 +97,13 @@ pub unsafe fn load_mut_unchecked<T: Transmutable>(
9797
}
9898
Ok(&mut *(bytes.as_mut_ptr() as *mut T))
9999
}
100+
101+
/// Validates a `COption` mask value.
102+
#[inline(always)]
103+
const fn validate_option(value: [u8; 4]) -> ProgramResult {
104+
if u32::from_le_bytes(value) > 1 {
105+
Err(ProgramError::InvalidAccountData)
106+
} else {
107+
Ok(())
108+
}
109+
}

0 commit comments

Comments
 (0)