Rust Fund

AI First Flight #9
Beginner FriendlyRust
EXP
View results
Submission Details
Impact: medium
Likelihood: medium
Invalid

contribute uses an unchecked fund.amount_raised += amount, which can overflow u64 (unlike the checked math used in refund/withdraw)

contribute uses unchecked += on amount_raised, risking silent overflow

Description

contribute (lib.rs:50) accumulates with the plain += operator instead of the checked arithmetic used everywhere else in the program (checked_sub in refund/withdraw, checked_add for lamport credits). If overflow checks are disabled in a release build, fund.amount_raised += amount wraps silently.

system_program::transfer(cpi_context, amount)?;
fund.amount_raised += amount; // @> unchecked add; wraps on overflow in release :50

Risk

Likelihood:

Low in practice — reaching u64::MAX lamports requires enormous or many contributions — but it is conditioned on the build's overflow-checks setting, which defaults to off in release. Inconsistent arithmetic style across the program makes the unchecked path easy to overlook.

Impact:

A wraparound corrupts amount_raised, the same value that drives goal evaluation and is paid out by withdraw (lib.rs:91). A wrapped-small amount_raised could make withdraw underpay, or interact with the withdraw checked_sub math, while a contributor's transferred lamports are already in the fund — breaking accounting integrity. Even where economically improbable, it is a latent correctness defect that should match the program's own checked-math convention.

Proof of Concept

Conceptually, drive amount_raised near u64::MAX (or build with overflow-checks = false) and contribute past the boundary.

// amount_raised = u64::MAX - 10
await program.methods.contribute(new BN(100)).accounts({...}).rpc();
// release build: amount_raised wraps to ~89 instead of erroring

Recommended Mitigation

Use checked_add and surface the existing overflow error, matching the rest of the program.

- fund.amount_raised += amount;
+ fund.amount_raised = fund.amount_raised
+ .checked_add(amount)
+ .ok_or(ErrorCode::CalculationOverflow)?;
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 3 hours ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!