Browse Source

Improve coption validation

febo 5 months ago
parent
commit
4f6e098cda
3 changed files with 19 additions and 23 deletions
  1. 5 13
      interface/src/state/account.rs
  2. 3 9
      interface/src/state/mint.rs
  3. 11 1
      interface/src/state/mod.rs

+ 5 - 13
interface/src/state/account.rs

@@ -1,5 +1,5 @@
 use {
-    super::{account_state::AccountState, COption, Initializable, Transmutable},
+    super::{account_state::AccountState, validate_option, COption, Initializable, Transmutable},
     pinocchio::{program_error::ProgramError, pubkey::Pubkey},
 };
 
@@ -159,24 +159,16 @@ impl Initializable for Account {
     #[inline(always)]
     fn is_initialized(&self) -> Result<bool, ProgramError> {
         // delegate
-        match self.delegate.0 {
-            [0, 0, 0, 0] | [1, 0, 0, 0] => (),
-            _ => return Err(ProgramError::InvalidAccountData),
-        }
+        validate_option(self.delegate.0)?;
+
         // state
         let state = AccountState::try_from(self.state)?;
 
         // is_native
-        match self.is_native {
-            [0, 0, 0, 0] | [1, 0, 0, 0] => (),
-            _ => return Err(ProgramError::InvalidAccountData),
-        }
+        validate_option(self.is_native)?;
 
         // close authority
-        match self.close_authority.0 {
-            [0, 0, 0, 0] | [1, 0, 0, 0] => (),
-            _ => return Err(ProgramError::InvalidAccountData),
-        }
+        validate_option(self.close_authority.0)?;
 
         Ok(state != AccountState::Uninitialized)
     }

+ 3 - 9
interface/src/state/mint.rs

@@ -1,5 +1,5 @@
 use {
-    super::{COption, Initializable, Transmutable},
+    super::{validate_option, COption, Initializable, Transmutable},
     pinocchio::{program_error::ProgramError, pubkey::Pubkey},
 };
 
@@ -93,10 +93,7 @@ impl Initializable for Mint {
     #[inline(always)]
     fn is_initialized(&self) -> Result<bool, ProgramError> {
         // mint_authority
-        match self.mint_authority.0 {
-            [0, 0, 0, 0] | [1, 0, 0, 0] => (),
-            _ => return Err(ProgramError::InvalidAccountData),
-        }
+        validate_option(self.mint_authority.0)?;
 
         // is_initialized
         let initialized = match self.is_initialized {
@@ -106,10 +103,7 @@ impl Initializable for Mint {
         };
 
         // freeze_authority
-        match self.freeze_authority.0 {
-            [0, 0, 0, 0] | [1, 0, 0, 0] => (),
-            _ => return Err(ProgramError::InvalidAccountData),
-        }
+        validate_option(self.freeze_authority.0)?;
 
         Ok(initialized)
     }

+ 11 - 1
interface/src/state/mod.rs

@@ -1,4 +1,4 @@
-use pinocchio::program_error::ProgramError;
+use pinocchio::{program_error::ProgramError, ProgramResult};
 
 pub mod account;
 pub mod account_state;
@@ -97,3 +97,13 @@ pub unsafe fn load_mut_unchecked<T: Transmutable>(
     }
     Ok(&mut *(bytes.as_mut_ptr() as *mut T))
 }
+
+/// Validates a `COption` mask value.
+#[inline(always)]
+const fn validate_option(value: [u8; 4]) -> ProgramResult {
+    if u32::from_le_bytes(value) > 1 {
+        Err(ProgramError::InvalidAccountData)
+    } else {
+        Ok(())
+    }
+}