Browse Source

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

Paul 3 years ago
parent
commit
adb90c33d7

+ 1 - 0
CHANGELOG.md

@@ -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
 

+ 27 - 17
lang/syn/src/codegen/program/dispatch.rs

@@ -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
             }
         }
     }

+ 1 - 1
tests/misc/tests/misc.ts

@@ -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",
     ];
 

+ 1 - 1
tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs

@@ -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(