fix: prevent panic on undersized zero-copy account deserialization (#4509)#4519
fix: prevent panic on undersized zero-copy account deserialization (#4509)#4519Abhilashpatel12 wants to merge 14 commits into
Conversation
|
@Abhilashpatel12 is attempting to deploy a commit to the Solana Foundation Team on Vercel. A member of the Team first needs to authorize it. |
Greptile SummaryThis PR prevents
Confidence Score: 4/5Safe to merge; all previously-panicking paths now return structured errors and are covered by new regression tests. The core panic-prevention logic is correct and well-tested. The main concern is that accounts shorter than the discriminator will now surface AccountDidNotDeserialize from load() and load_mut() instead of AccountDiscriminatorNotFound, silently changing the error variant visible to any caller that matched on it. lang/src/accounts/account_loader.rs — the error code returned for the sub-discriminator case changed and that change is not called out in the PR description or inline docs. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["AccountLoader::load / load_mut / load_init"] --> B["try_borrow_data"]
B --> C{"data.len() less than disc+size_of T?"}
C -- YES --> D["Err: AccountDidNotDeserialize NEW"]
C -- NO --> E{"disc bytes match?"}
E -- NO --> F["Err: AccountDiscriminatorMismatch"]
E -- YES --> G["bytemuck::from_bytes safe cast"]
H["#account zero - generate_constraint_zeroed"] --> I["try_borrow_data"]
I --> J{"data.len() less than disc.len()?"}
J -- YES --> K["Err: AccountDidNotDeserialize NEW"]
J -- NO --> L{"discriminator all-zero?"}
L -- NO --> M["Err: ConstraintZero"]
L -- YES --> N["from_account_info"]
N --> O["try_from_unchecked - owner check only"]
O --> P["User calls load_init later"]
P --> C
|
|
There's a lot of repeated checks here, can we factor this out? |
|
Good point , I’ll factor the repeated validation logic into shared private helper methods in account_loader.rs to reduce duplication while preserving the existing error behavior. I’ll also clean up the discriminator-length naming in the generated constraint path for clarity. |
|
@jamie-osec please checkout the updated commits |
Co-authored-by: Jamie Hill-Daniel <134328753+jamie-osec@users.noreply.github.com>
|
@jamie-osec tests are passed needed authorization to pass the vercel test |
This PR fixes panic-triggerable zero-copy deserialization paths for under-sized accounts in AccountLoader::{load, load_mut, load_init} and generated #[account(zero)] validation code