Skip to content

Commit

Permalink
Add discriminator length checks instead of panicking in `(Account)Loa…
Browse files Browse the repository at this point in the history
…der` (#1678)
  • Loading branch information
smoelius authored Mar 24, 2022
1 parent 23efadf commit 0f7675c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The minor version will be incremented upon a breaking change and the patch versi
### Fixes

* avm: `avm install` no longer downloads the version if already installed in the machine ([#1670](https://github.com/project-serum/anchor/pull/1670)).
* lang: Return proper error instead of panicking if account length is smaller than discriminator in functions of `(Account)Loader` ([#1678](https://github.com/project-serum/anchor/pull/1678)).

### Breaking

Expand Down
9 changes: 9 additions & 0 deletions lang/src/accounts/account_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
.with_pubkeys((*acc_info.owner, T::owner())));
}
let data: &[u8] = &acc_info.try_borrow_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}
// Discriminator must match.
let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand All @@ -149,6 +152,9 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
/// Returns a Ref to the account data structure for reading.
pub fn load(&self) -> Result<Ref<T>> {
let data = self.acc_info.try_borrow_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}

let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand All @@ -169,6 +175,9 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
}

let data = self.acc_info.try_borrow_mut_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}

let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand Down
9 changes: 9 additions & 0 deletions lang/src/accounts/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
.with_pubkeys((*acc_info.owner, *program_id)));
}
let data: &[u8] = &acc_info.try_borrow_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}
// Discriminator must match.
let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand Down Expand Up @@ -90,6 +93,9 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
#[allow(deprecated)]
pub fn load(&self) -> Result<Ref<T>> {
let data = self.acc_info.try_borrow_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}

let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand All @@ -109,6 +115,9 @@ impl<'info, T: ZeroCopy> Loader<'info, T> {
}

let data = self.acc_info.try_borrow_mut_data()?;
if data.len() < T::discriminator().len() {
return Err(ErrorCode::AccountDiscriminatorNotFound.into());
}

let disc_bytes = array_ref![data, 0, 8];
if disc_bytes != &T::discriminator() {
Expand Down

0 comments on commit 0f7675c

Please sign in to comment.