Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 0ff8fe1

Browse files
authored
token-2022: Error sooner when searching for an extension in TLV data (#3761)
1 parent 836c9e6 commit 0ff8fe1

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

token/program-2022/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ pub enum TokenError {
187187
/// Account ownership cannot be changed while CPI Guard is enabled
188188
#[error("Account ownership cannot be changed while CPI Guard is enabled")]
189189
CpiGuardOwnerChangeBlocked,
190+
/// Extension not found in account data
191+
#[error("Extension not found in account data")]
192+
ExtensionNotFound,
190193
}
191194
impl From<TokenError> for ProgramError {
192195
fn from(e: TokenError) -> Self {
@@ -323,6 +326,9 @@ impl PrintProgramError for TokenError {
323326
TokenError::CpiGuardOwnerChangeBlocked => {
324327
msg!("Account ownership cannot be changed while CPI Guard is enabled")
325328
}
329+
TokenError::ExtensionNotFound => {
330+
msg!("Extension not found in account data")
331+
}
326332
}
327333
}
328334
}

token/program-2022/src/extension/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,17 @@ fn get_extension_indices<V: Extension>(
104104
let extension_type =
105105
ExtensionType::try_from(&tlv_data[tlv_indices.type_start..tlv_indices.length_start])?;
106106
let account_type = extension_type.get_account_type();
107-
// got to an empty spot, can init here, or move forward if not initing
108-
if extension_type == ExtensionType::Uninitialized {
107+
if extension_type == V::TYPE {
108+
// found an instance of the extension that we're initializing, return!
109+
return Ok(tlv_indices);
110+
// got to an empty spot, init here, or error if we're searching, since
111+
// nothing is written after an Uninitialized spot
112+
} else if extension_type == ExtensionType::Uninitialized {
109113
if init {
110114
return Ok(tlv_indices);
111115
} else {
112-
start_index = tlv_indices.length_start;
116+
return Err(TokenError::ExtensionNotFound.into());
113117
}
114-
} else if extension_type == V::TYPE {
115-
// found an instance of the extension that we're initializing, return!
116-
return Ok(tlv_indices);
117118
} else if v_account_type != account_type {
118119
return Err(TokenError::ExtensionTypeMismatch.into());
119120
} else {
@@ -1603,8 +1604,14 @@ mod test {
16031604
state.base = TEST_ACCOUNT;
16041605
state.pack_base();
16051606
state.init_account_type().unwrap();
1606-
state.init_extension::<ImmutableOwner>(true).unwrap();
16071607

1608+
let err = state.get_extension::<ImmutableOwner>().unwrap_err();
1609+
assert_eq!(
1610+
err,
1611+
ProgramError::Custom(TokenError::ExtensionNotFound as u32)
1612+
);
1613+
1614+
state.init_extension::<ImmutableOwner>(true).unwrap();
16081615
assert_eq!(
16091616
get_first_extension_type(state.tlv_data).unwrap(),
16101617
Some(ExtensionType::ImmutableOwner)

0 commit comments

Comments
 (0)