@@ -47,27 +47,28 @@ pub fn write(accounts: &mut [AccountView], instruction_data: &[u8]) -> ProgramRe
4747 return Err ( ProgramError :: InvalidAccountData ) ;
4848 }
4949
50- // SAFETY: `buffer` account data is guaranteed to be a `Buffer`.
51- let buffer_header = unsafe { Buffer :: from_bytes_unchecked ( data) } ;
50+ // `data` was validated to have a `Buffer` discriminator .
51+ let buffer_header = Buffer :: from_bytes ( data) ? ;
5252
5353 if Some ( authority. address ( ) ) != buffer_header. authority . as_ref ( ) {
5454 return Err ( ProgramError :: IncorrectAuthority ) ;
5555 }
5656
57- // Determine from where to copy the data.
57+ // Determine from where to copy the data, ether from the instruction data
58+ // or the source buffer account.
5859 let instruction_data = match args. data ( ) {
5960 source_data if !source_data. is_empty ( ) => Some ( source_data) ,
6061 _ => None ,
6162 } ;
6263
63- let buffer_data = if source_buffer. address ( ) != & crate :: ID {
64+ let source_buffer_data = if source_buffer. address ( ) != & crate :: ID {
6465 // SAFETY: singe immutable borrow of `source_buffer` account data.
6566 Some ( unsafe { source_buffer. borrow_unchecked ( ) } )
6667 } else {
6768 None
6869 } ;
6970
70- let source_data = match ( instruction_data, buffer_data ) {
71+ let source_data = match ( instruction_data, source_buffer_data ) {
7172 ( Some ( instruction_data) , None ) => instruction_data,
7273 ( None , Some ( buffer_data) ) => match AccountDiscriminator :: try_from_bytes ( buffer_data) ? {
7374 Some ( AccountDiscriminator :: Buffer ) => & buffer_data[ Header :: LEN ..] ,
@@ -76,7 +77,8 @@ pub fn write(accounts: &mut [AccountView], instruction_data: &[u8]) -> ProgramRe
7677 _ => return Err ( ProgramError :: InvalidInstructionData ) ,
7778 } ;
7879
79- // The length of the data to write is validated by the `try_minimum_balance`.
80+ // The length of the data to write is validated by the `try_minimum_balance` and
81+ // `offset` is a `u32` value and `source_data` is at most `10_000_000` bytes.
8082 ( max ( data. len ( ) , offset + source_data. len ( ) ) , source_data)
8183 } ;
8284
0 commit comments