Skip to main content
Sigvex

Rent Exempt Reallocation Remediation

How to fix account reallocations without rent-exemption adjustments.

Rent Exempt Reallocation Remediation

Overview

Detector Reference: Rent Exempt Reallocation

This guide explains how to maintain rent exemption when resizing Solana accounts.

Calculate the rent-exempt minimum for the new size and adjust lamports accordingly:

let rent = Rent::get()?;
let required = rent.minimum_balance(new_size);
let current = account.lamports();

account.realloc(new_size, true)?;

if required > current {
    let shortfall = required - current;
    **payer.try_borrow_mut_lamports()? -= shortfall;
    **account.try_borrow_mut_lamports()? += shortfall;
} else if current > required {
    let excess = current - required;
    **account.try_borrow_mut_lamports()? -= excess;
    **destination.try_borrow_mut_lamports()? += excess;
}

Alternative Mitigations

  1. Anchor realloc constraint: #[account(mut, realloc = size, realloc::payer = payer, realloc::zero = true)] handles rent automatically.
  2. Over-fund conservatively: allocate extra lamports at creation to cover potential future growth.
  3. Reject shrinking: if your program does not need to shrink accounts, reject realloc requests that reduce size.

Common Mistakes

  • Adjusting lamports before realloc: the new minimum balance depends on the new size, so calculate it first but transfer after realloc succeeds.
  • Forgetting to return excess on shrink: users lose lamports trapped in over-funded accounts.
  • Using stale rent values: always call Rent::get() fresh rather than using cached values.

References