-
Notifications
You must be signed in to change notification settings - Fork 9
fix: prevent arithmetic overflow crash #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
raulriera
wants to merge
3
commits into
refactor-new-server
Choose a base branch
from
fix-crash-arithmetic-overflow
base: refactor-new-server
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
FlipcashTests/Regressions/Regression_698ef3b65e6cc4bb5554e13d.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // | ||
| // Regression_698ef3b65e6cc4bb5554e13d.swift | ||
| // Flipcash | ||
| // | ||
| // Crash: UInt64.scaleUp arithmetic overflow when comparing Quarks | ||
| // with different decimal precisions for high-rate currencies (CLP). | ||
| // | ||
| // Fix: Quarks.< now compares via decimalValue (Decimal) instead of | ||
| // aligning UInt64 quarks to a common precision via scaleUp. | ||
| // | ||
|
|
||
| import Foundation | ||
| import Testing | ||
| import FlipcashCore | ||
| @testable import Flipcash | ||
|
|
||
| @Suite("Regression: 698ef3b – Quarks comparison overflow for high-rate currencies") | ||
| struct Regression_698ef3b { | ||
|
|
||
| @Test("CLP quarks comparison across 6 and 10 decimal precisions does not overflow") | ||
| func quarksComparison_CLP_doesNotOverflow() { | ||
| // clpAt6: 475_000_000_000_000 / 10^6 = 475,000,000 CLP | ||
| // clpAt10: 1_000_000_000_000_000_000 / 10^10 = 100,000,000 CLP | ||
| let clpAt6 = Quarks(quarks: 475_000_000_000_000 as UInt64, currencyCode: .clp, decimals: 6) | ||
| let clpAt10 = Quarks(quarks: 1_000_000_000_000_000_000 as UInt64, currencyCode: .clp, decimals: 10) | ||
|
|
||
| // Must not crash, and 100M < 475M | ||
| #expect(clpAt10 < clpAt6) | ||
| } | ||
|
|
||
| @Test("EnterAmountCalculator.maxEnterAmount with CLP does not overflow") | ||
| func maxEnterAmount_CLP_doesNotOverflow() { | ||
| let clpRate = Rate(fx: 950, currency: .clp) | ||
|
|
||
| let underlying = Quarks(quarks: 500_000_000 as UInt64, currencyCode: .usd, decimals: 6) | ||
| let converted = Quarks(quarks: 475_000_000_000 as UInt64, currencyCode: .clp, decimals: 6) | ||
| let balance = ExchangedFiat( | ||
| underlying: underlying, | ||
| converted: converted, | ||
| rate: clpRate, | ||
| mint: .usdf | ||
| ) | ||
|
|
||
| let limit = Quarks(quarks: 950_000_000_000 as UInt64, currencyCode: .clp, decimals: 6) | ||
|
|
||
| let calculator = EnterAmountCalculator( | ||
| mode: .currency, | ||
| entryCurrency: .clp, | ||
| onrampCurrency: .usd, | ||
| transactionLimitProvider: { _ in return limit }, | ||
| rateProvider: { _ in clpRate } | ||
| ) | ||
|
|
||
| // Must not crash with arithmetic overflow | ||
| let result = calculator.maxEnterAmount(maxBalance: balance) | ||
| #expect(result == balance.converted) | ||
| } | ||
|
|
||
| @Test( | ||
| "No currency overflows when comparing quarks across 6 and 10 decimal precisions", | ||
| arguments: CurrencyCode.allCases | ||
| ) | ||
| func quarksComparison_allCurrencies_noOverflow(currency: CurrencyCode) { | ||
| let largeAt6 = Quarks(quarks: 1_000_000_000_000_000 as UInt64, currencyCode: currency, decimals: 6) | ||
| let largeAt10 = Quarks(quarks: 1_000_000_000_000_000 as UInt64, currencyCode: currency, decimals: 10) | ||
|
|
||
| // 10^15 / 10^6 = 10^9 vs 10^15 / 10^10 = 10^5 | ||
| #expect(largeAt10 < largeAt6) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // | ||
| // UInt64OperationsTests.swift | ||
| // Flipcash | ||
| // | ||
| // Created by Claude on 2026-02-13. | ||
| // | ||
|
|
||
| import Foundation | ||
| import Testing | ||
| @testable import FlipcashCore | ||
|
|
||
| // MARK: - scaleDown | ||
|
|
||
| @Suite("UInt64.scaleDown") | ||
| struct UInt64ScaleDownTests { | ||
|
|
||
| @Test("Converts quarks to Decimal by dividing by 10^d") | ||
| func convertsQuarksToDecimal() { | ||
| #expect((123_456_789 as UInt64).scaleDown(6) == Decimal(string: "123.456789")) | ||
| } | ||
|
|
||
| @Test("Value smaller than factor returns a fractional Decimal") | ||
| func smallValue() { | ||
| #expect((1 as UInt64).scaleDown(6) == Decimal(string: "0.000001")) | ||
| } | ||
|
|
||
| @Test("Preserves precision for high-rate currency quarks") | ||
| func highRateCurrencyQuarks() { | ||
| // CLP-scale quarks used in Quarks.converting(to:decimals:) | ||
| #expect((475_000_000_000_000 as UInt64).scaleDown(6) == Decimal(475_000_000)) | ||
| } | ||
|
|
||
| @Test("Works with bonded token precision (10 decimals)") | ||
| func bondedTokenDecimals() { | ||
| #expect((1_000_000_000_000 as UInt64).scaleDown(10) == Decimal(100)) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - scaleDownInt | ||
|
|
||
| @Suite("UInt64.scaleDownInt") | ||
| struct UInt64ScaleDownIntTests { | ||
|
|
||
| @Test("Performs integer division by 10^d") | ||
| func integerDivision() { | ||
| #expect((123_456_789 as UInt64).scaleDownInt(4) == 12_345) | ||
| } | ||
|
|
||
| @Test("Truncates rather than rounds") | ||
| func truncates() { | ||
| #expect((999_999 as UInt64).scaleDownInt(6) == 0) | ||
| } | ||
|
|
||
| @Test("Exact match returns whole units") | ||
| func exactMatch() { | ||
| #expect((5_000_000 as UInt64).scaleDownInt(6) == 5) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - scaleUp | ||
|
|
||
| @Suite("UInt64.scaleUp") | ||
| struct UInt64ScaleUpTests { | ||
|
|
||
| @Test("Multiplies by 10^d") | ||
| func multipliesByFactor() { | ||
| #expect((123 as UInt64).scaleUp(4) == 1_230_000) | ||
| } | ||
|
|
||
| @Test("Zero value stays zero regardless of exponent") | ||
| func zeroValue() { | ||
| #expect((0 as UInt64).scaleUp(10) == 0) | ||
| } | ||
|
|
||
| @Test("Succeeds near the UInt64 boundary without overflow") | ||
| func maxSafeValue() { | ||
| // UInt64.max / 10_000 ≈ 1_844_674_407_370_955 | ||
| let quarks: UInt64 = 1_844_674_407_370_955 | ||
| #expect(quarks.scaleUp(4) == 18_446_744_073_709_550_000) | ||
| } | ||
|
|
||
| // MARK: - Roundtrip | ||
|
|
||
| @Test("scaleUp and scaleDownInt are inverse operations") | ||
| func roundtrip() { | ||
| let original: UInt64 = 475_000 | ||
| let scaled = original.scaleUp(6) | ||
|
|
||
| #expect(scaled == 475_000_000_000) | ||
| #expect(scaled.scaleDownInt(6) == original) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was preventing the tests from running (I really need to start running the tests in Xcode Cloud)