Skip to content

fix: Use checked pagination arithmetic in provenance::get_by_address #866

Description

@Samuel1505

Description

// provenance.rs:86-118
pub fn get_by_address(env: &Env, address: &Address, offset: u32, limit: u32) -> Vec<ProvenanceRecord> {
    ...
    let end = if offset + limit > len { len } else { offset + limit };
    ...
}

Unlike pagination::paginate (the shared helper documented as the canonical place "any module that needs offset/limit pagination" should use, and already used by registry.rs), get_by_address reimplements pagination inline. It doesn't clamp limit to MAX_PAGE_SIZE, and it computes offset + limit with plain u32 addition before checking against len. A caller passing large offset/limit values (e.g. both near u32::MAX) causes offset + limit to overflow, which panics in any build with overflow checks enabled — violating this codebase's "never panic!, return Result" convention — rather than returning a clamped/empty result the way pagination::paginate would.

Technical Requirements

Files to update

  • contracts/contracts/stellar-grants/src/provenance.rs (get_by_address, lines 86-118)

Fix direction

Replace the inline pagination logic with the shared pagination::paginate helper (or, at minimum, use saturating_add/checked_add and clamp against MAX_PAGE_SIZE the same way pagination::paginate does) so behavior is consistent with the rest of the codebase and can't panic on adversarial input:

let end = offset.saturating_add(limit).min(len);

Acceptance Criteria

  • get_by_address no longer panics when called with offset/limit values that would overflow u32 addition.
  • get_by_address's pagination behavior (clamping to available records, respecting a max page size) matches pagination::paginate's conventions.
  • A test calls get_by_address with offset/limit near u32::MAX and confirms it returns cleanly instead of panicking.
  • cargo test passes.

Estimated Effort

Beginner: 2 hours
Intermediate: 1 hour
Expert: 0.5 hours

How to work this issue

  1. Read contracts/ContributionGuide.md for the contribution workflow.
  2. Comment on the issue to claim it before starting.
  3. Branch: fix/issue-921-provenance-pagination-overflow.
  4. Run cargo fmt, cargo clippy -- -D warnings, cargo test before opening your PR.
  5. Use a Conventional Commit message, e.g. fix: use checked pagination arithmetic in provenance::get_by_address.

Before you start

If you find this project interesting, please consider starring the repository on GitHub. It helps the project gain visibility and supports the Drips Wave program that rewards contributors for merged fixes like this one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions