lang: Make Anchor use fallback function instead of panicking if ix data.len() is < `8` (#1721)

This commit is contained in:
Paul 2022-04-02 22:27:39 -04:00 committed by GitHub
parent 4ebcc5fb7e
commit adb90c33d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 19 deletions

View File

@ -27,6 +27,7 @@ The minor version will be incremented upon a breaking change and the patch versi
* avm: `amv install` switches to the newly installed version after installation finishes ([#1670](https://github.com/project-serum/anchor/pull/1670)).
* spl: Re-export the `spl_token` crate ([#1665](https://github.com/project-serum/anchor/pull/1665)).
* lang, cli, spl: Update solana toolchain to v1.9.13 ([#1653](https://github.com/project-serum/anchor/pull/1653)).
* lang: Use fallback function if ix data length smaller than `8` instead of panicking ([#1721](https://github.com/project-serum/anchor/pull/1721)).
## [0.23.0] - 2022-03-20

View File

@ -143,33 +143,43 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
// Split the instruction data into the first 8 byte method
// identifier (sighash) and the serialized instruction data.
let mut ix_data: &[u8] = data;
let sighash: [u8; 8] = {
let sighash: Option<[u8; 8]> = {
let mut sighash: [u8; 8] = [0; 8];
sighash.copy_from_slice(&ix_data[..8]);
ix_data = &ix_data[8..];
sighash
if ix_data.len() < 8 {
None
} else {
sighash.copy_from_slice(&ix_data[..8]);
ix_data = &ix_data[8..];
Some(sighash)
}
};
// If the method identifier is the IDL tag, then execute an IDL
// instruction, injected into all Anchor programs.
if cfg!(not(feature = "no-idl")) {
if sighash == anchor_lang::idl::IDL_IX_TAG.to_le_bytes() {
return __private::__idl::__idl_dispatch(
program_id,
accounts,
&ix_data,
);
if let Some(sighash) = sighash {
if sighash == anchor_lang::idl::IDL_IX_TAG.to_le_bytes() {
return __private::__idl::__idl_dispatch(
program_id,
accounts,
&ix_data,
);
}
}
}
match sighash {
#ctor_state_dispatch_arm
#(#state_dispatch_arms)*
#(#trait_dispatch_arms)*
#(#global_dispatch_arms)*
_ => {
#fallback_fn
Some(sighash) => {
match sighash {
#ctor_state_dispatch_arm
#(#state_dispatch_arms)*
#(#trait_dispatch_arms)*
#(#global_dispatch_arms)*
_ => {
#fallback_fn
}
}
}
None => #fallback_fn
}
}
}

View File

@ -160,7 +160,7 @@ describe("misc", () => {
"Program data: jvbowsvlmkcJAAAA",
"Program data: zxM5neEnS1kBAgMEBQYHCAkK",
"Program data: g06Ei2GL1gIBAgMEBQYHCAkKCw==",
"Program 3TEqcc8xhrhdspwbvoamUJe2borm4Nr72JxL66k6rgrh consumed 5395 of 1400000 compute units",
"Program 3TEqcc8xhrhdspwbvoamUJe2borm4Nr72JxL66k6rgrh consumed 5418 of 1400000 compute units",
"Program 3TEqcc8xhrhdspwbvoamUJe2borm4Nr72JxL66k6rgrh success",
];

View File

@ -38,7 +38,7 @@ async fn update_foo() {
let mut pt = ProgramTest::new("zero_copy", zero_copy::id(), None);
pt.add_account(foo_pubkey, foo_account);
pt.set_compute_max_units(3157);
pt.set_compute_max_units(3174);
let (mut banks_client, payer, recent_blockhash) = pt.start().await;
let client = Client::new_with_options(