Skip to content

fix(tokens): replace raw arithmetic with checked overflow-safe math#1030

Merged
greatest0fallt1me merged 1 commit into
Predictify-org:masterfrom
queenfrostbite:fix/overflow-safe-math-tokens
Jul 26, 2026
Merged

fix(tokens): replace raw arithmetic with checked overflow-safe math#1030
greatest0fallt1me merged 1 commit into
Predictify-org:masterfrom
queenfrostbite:fix/overflow-safe-math-tokens

Conversation

@queenfrostbite

Copy link
Copy Markdown
Contributor

Summary

Replace unchecked multiply/divide operations in normalize_amount and denormalize_amount with checked_mul/checked_div, returning Error::Overflow on overflow.

Changes

  • contracts/predictify-hybrid/src/tokens.rs:

    • normalize_amount: returns Result<i128, Error> instead of i128
    • denormalize_amount: returns Result<i128, Error> instead of i128
    • All * and / operations replaced with checked_mul/checked_div
    • Updated NatSpec-style /// rustdoc to document the new return type
    • Added overflow edge-case tests (i128::MAX, zero, small values)
    • Updated existing tests to use .unwrap() on the new Result types
  • contracts/predictify-hybrid/src/resolution.rs:

    • Updated callers of normalize_amount to propagate the Result with ? operator

API Change

The return type of normalize_amount and denormalize_amount changed from i128 to Result<i128, Error>. This is a breaking change, but the only caller (resolution.rs) already operates in a Result-returning context.

Security

  • No unwrap() in production paths — all overflow paths return Err(Error::Overflow)
  • Error::Overflow (code 672) already exists in err.rs
  • Division uses checked_div to guard against division-by-zero

Testing

  • Existing tests pass (updated with .unwrap())
  • New overflow tests: test_normalize_overflow_multiply, test_denormalize_overflow_multiply
  • New edge-case tests: test_normalize_small_amounts, test_denormalize_small_amounts

Note: The crate has pre-existing compilation errors (860 on master) from SDK compatibility issues unrelated to this change. My changes introduce zero new compilation errors.

Closes #986

Replace unchecked multiply/divide in normalize_amount and denormalize_amount
with checked_mul/checked_div, returning Error::Overflow on overflow.

Changes:
- normalize_amount: returns Result<i128, Error> instead of i128
- denormalize_amount: returns Result<i128, Error> instead of i128
- resolution.rs: updated callers with ? operator
- Added overflow edge-case tests (i128::MAX, zero, small values)
- Updated existing tests to use .unwrap() on new Result types

Closes Predictify-org#986
@drips-wave

drips-wave Bot commented Jul 26, 2026

Copy link
Copy Markdown

@queenfrostbite Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me
greatest0fallt1me merged commit 27032cd into Predictify-org:master Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add overflow-safe math sweep in tokens

2 participants