From 8f7c05c7cf63d214df18aeb356ec5de29051391e Mon Sep 17 00:00:00 2001 From: ara <226676426+ara-stock@users.noreply.github.com> Date: Sat, 10 Oct 2026 12:27:56 +0900 Subject: [PATCH] program: Return an error instead of panicking on Reallocate overflow Reallocate added the record header size to the requested data length with checked_add(...).unwrap(), so a data_length close to u64::MAX made the program panic instead of returning InvalidArgument. Co-Authored-By: Claude Opus 5.5 --- program/src/processor.rs | 2 +- program/tests/functional.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/program/src/processor.rs b/program/src/processor.rs index 477013f..f435403 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -161,7 +161,7 @@ pub fn process_instruction( .checked_add( usize::try_from(data_length).map_err(|_| ProgramError::InvalidArgument)?, ) - .unwrap(); + .ok_or(ProgramError::InvalidArgument)?; // reallocate if data_info.data_len() >= needed_account_length { diff --git a/program/tests/functional.rs b/program/tests/functional.rs index 6454e4b..0fa0338 100644 --- a/program/tests/functional.rs +++ b/program/tests/functional.rs @@ -730,3 +730,38 @@ fn reallocate_fail_unsigned() { ], ); } + +#[test] +fn reallocate_fail_data_length_overflow() { + let mollusk = Mollusk::new(&id(), "spl_record"); + let payer = Address::new_unique(); + let authority = Address::new_unique(); + let account = Address::new_unique(); + let data = &[111u8; 8]; + let mut ixs = initialize_instructions(&payer, &authority, &account, data).to_vec(); + ixs.push(instruction::reallocate(&account, &authority, u64::MAX)); + let checks = [ + [Check::success()], + [Check::success()], + [Check::success()], + [Check::err(ProgramError::InvalidArgument)], + ]; + mollusk.process_and_validate_instruction_chain( + ixs.iter() + .zip(checks.iter().map(|c| c.as_ref())) + .collect::>() + .as_slice(), + &[ + ( + payer, + Account { + lamports: 1_000_000_000, + ..Default::default() + }, + ), + (authority, Account::default()), + (account, Account::default()), + mollusk_svm::program::keyed_account_for_system_program(), + ], + ); +}