Skip to content

Commit d84197b

Browse files
authored
Ignore zero_init parameter (#203)
Ignore zero_init parameter
1 parent ca3a98b commit d84197b

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

sdk/pinocchio/src/account_info.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -456,32 +456,42 @@ impl AccountInfo {
456456
Ok(())
457457
}
458458

459-
/// Realloc the account's data and optionally zero-initialize the new
460-
/// memory.
459+
/// Realloc (either truncating or zero extending) the account's data.
461460
///
462461
/// The account data can be increased by up to [`MAX_PERMITTED_DATA_INCREASE`] bytes
463462
/// within an instruction.
464463
///
465464
/// # Important
466465
///
467-
/// Memory used to grow is already zero-initialized upon program
468-
/// entrypoint and re-zeroing it wastes compute units. If within the same
469-
/// call a program reallocs from larger to smaller and back to larger again
470-
/// the new space could contain stale data. Pass `true` for `zero_init` in
471-
/// this case, otherwise compute units will be wasted re-zero-initializing.
466+
/// The use of the `zero_init` parameter, which indicated whether the newly
467+
/// allocated memory should be zero-initialized or not, is now deprecated and
468+
/// ignored. The method will always zero-initialize the newly allocated memory
469+
/// if the new length is larger than the current data length. This is the same
470+
/// behavior as [`Self::resize`].
472471
///
473-
/// Note that `zero_init` being `false` will not be supported by the
474-
/// program runtime in the future. Programs should not rely on being able to
475-
/// access stale data and use [`Self::resize`] instead.
472+
/// This method makes assumptions about the layout and location of memory
473+
/// referenced by `AccountInfo` fields. It should only be called for
474+
/// instances of `AccountInfo` that were created by the runtime and received
475+
/// in the `process_instruction` entrypoint of a program.
476+
#[deprecated(since = "0.9.0", note = "Use AccountInfo::resize() instead")]
477+
#[inline(always)]
478+
pub fn realloc(&self, new_len: usize, _zero_init: bool) -> Result<(), ProgramError> {
479+
self.resize(new_len)
480+
}
481+
482+
/// Resize (either truncating or zero extending) the account's data.
476483
///
477-
/// # Safety
484+
/// The account data can be increased by up to [`MAX_PERMITTED_DATA_INCREASE`] bytes
485+
/// within an instruction.
486+
///
487+
/// # Important
478488
///
479489
/// This method makes assumptions about the layout and location of memory
480490
/// referenced by `AccountInfo` fields. It should only be called for
481491
/// instances of `AccountInfo` that were created by the runtime and received
482492
/// in the `process_instruction` entrypoint of a program.
483-
#[deprecated(since = "0.9.0", note = "Use AccountInfo::resize() instead")]
484-
pub fn realloc(&self, new_len: usize, zero_init: bool) -> Result<(), ProgramError> {
493+
#[inline]
494+
pub fn resize(&self, new_len: usize) -> Result<(), ProgramError> {
485495
// Check wheather the account data is already borrowed.
486496
self.can_borrow_mut_data()?;
487497

@@ -509,7 +519,7 @@ impl AccountInfo {
509519
(*self.raw).resize_delta = accumulated_resize_delta;
510520
}
511521

512-
if zero_init && difference > 0 {
522+
if difference > 0 {
513523
unsafe {
514524
#[cfg(target_os = "solana")]
515525
sol_memset_(
@@ -529,23 +539,6 @@ impl AccountInfo {
529539
Ok(())
530540
}
531541

532-
/// Resize (either truncating or zero extending) the account's data.
533-
///
534-
/// The account data can be increased by up to [`MAX_PERMITTED_DATA_INCREASE`] bytes
535-
/// within an instruction.
536-
///
537-
/// # Important
538-
///
539-
/// This method makes assumptions about the layout and location of memory
540-
/// referenced by `AccountInfo` fields. It should only be called for
541-
/// instances of `AccountInfo` that were created by the runtime and received
542-
/// in the `process_instruction` entrypoint of a program.
543-
#[inline]
544-
pub fn resize(&self, new_len: usize) -> Result<(), ProgramError> {
545-
#[allow(deprecated)]
546-
self.realloc(new_len, true)
547-
}
548-
549542
/// Zero out the the account's data length, lamports and owner fields, effectively
550543
/// closing the account.
551544
///

0 commit comments

Comments
 (0)