Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 79a2cca

Browse files
Return an error from create_program_address syscall (bp #11658) (#11789)
* Return an error from create_program_address syscall (#11658) (cherry picked from commit 750e534) # Conflicts: # programs/bpf_loader/src/syscalls.rs * resolve conflicts * resolve conflicts Co-authored-by: Jack May <jack@solana.com>
1 parent f5e583e commit 79a2cca

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

programs/bpf/rust/invoke/src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use solana_sdk::{
1212
info,
1313
program::{invoke, invoke_signed},
1414
program_error::ProgramError,
15-
pubkey::Pubkey,
15+
pubkey::{Pubkey, PubkeyError},
1616
system_instruction,
1717
};
1818

@@ -97,9 +97,15 @@ fn process_instruction(
9797

9898
info!("Test create_program_address");
9999
{
100-
let address =
101-
Pubkey::create_program_address(&[b"You pass butter", &[nonce1]], program_id)?;
102-
assert_eq!(&address, accounts[DERIVED_KEY1_INDEX].key);
100+
assert_eq!(
101+
&Pubkey::create_program_address(&[b"You pass butter", &[nonce1]], program_id)?,
102+
accounts[DERIVED_KEY1_INDEX].key
103+
);
104+
assert_eq!(
105+
Pubkey::create_program_address(&[b"You pass butter"], &Pubkey::default())
106+
.unwrap_err(),
107+
PubkeyError::InvalidSeeds
108+
);
103109
}
104110

105111
info!("Test derived signers");

programs/bpf_loader/src/syscalls.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ pub fn syscall_create_program_address(
342342
ro_regions: &[MemoryRegion],
343343
rw_regions: &[MemoryRegion],
344344
) -> Result<u64, EbpfError<BPFError>> {
345-
let untranslated_seeds = translate_slice!(&[&str], seeds_addr, seeds_len, ro_regions)?;
345+
let untranslated_seeds = translate_slice!(&[&u8], seeds_addr, seeds_len, ro_regions)?;
346346
let seeds = untranslated_seeds
347347
.iter()
348348
.map(|untranslated_seed| {
@@ -356,7 +356,10 @@ pub fn syscall_create_program_address(
356356
.collect::<Result<Vec<_>, EbpfError<BPFError>>>()?;
357357
let program_id = translate_type!(Pubkey, program_id_addr, ro_regions)?;
358358
let new_address =
359-
Pubkey::create_program_address(&seeds, program_id).map_err(SyscallError::BadSeeds)?;
359+
match Pubkey::create_program_address(&seeds, program_id).map_err(SyscallError::BadSeeds) {
360+
Ok(address) => address,
361+
Err(_) => return Ok(1),
362+
};
360363
let address = translate_slice_mut!(u8, address_addr, 32, rw_regions)?;
361364
address.copy_from_slice(new_address.as_ref());
362365
Ok(0)

0 commit comments

Comments
 (0)