-
Notifications
You must be signed in to change notification settings - Fork 1
fix: Prevent arithmetic overflow and underflow #486
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package to.bitkit.models | ||
|
|
||
| /** | ||
| * A wrapper for [ULong] that provides saturating arithmetic operations. | ||
| * All operations prevent overflow/underflow by clamping to valid range [0, [ULong.MAX_VALUE]]. | ||
| * Similar to Rust's saturating arithmetic (e.g., `x.saturating_sub(y)`). | ||
| */ | ||
| @JvmInline | ||
| value class USat(val value: ULong) : Comparable<USat> { | ||
|
|
||
| override fun compareTo(other: USat): Int = value.compareTo(other.value) | ||
|
|
||
| /** Saturating subtraction: returns 0 if result would be negative. */ | ||
| operator fun minus(other: USat): ULong = | ||
| if (value >= other.value) value - other.value else 0uL | ||
|
|
||
| /** Saturating addition: caps at ULong.MAX_VALUE if result would overflow. */ | ||
| operator fun plus(other: USat): ULong = | ||
| if (value <= ULong.MAX_VALUE - other.value) value + other.value else ULong.MAX_VALUE | ||
| } | ||
|
|
||
| /** | ||
| * Wraps this ULong in a [USat] for saturating arithmetic operations. | ||
| * Use this when performing arithmetic that could overflow/underflow. | ||
| * | ||
| * Example: | ||
| * ``` | ||
| * val result = a.safe() - b.safe() // Returns 0 if a < b instead of wrapping | ||
| * ``` | ||
| */ | ||
| fun ULong.safe(): USat = USat(this) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package to.bitkit.models | ||
|
|
||
| import org.junit.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class USatTest { | ||
|
|
||
| // region Subtraction | ||
| @Test | ||
| fun `minus returns difference when a greater than b`() { | ||
| val result = USat(10uL) - USat(5uL) | ||
| assertEquals(5uL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `minus returns zero when a equals b`() { | ||
| val result = USat(5uL) - USat(5uL) | ||
| assertEquals(0uL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `minus returns zero when would underflow`() { | ||
| val result = USat(5uL) - USat(10uL) | ||
| assertEquals(0uL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `minus handles max ULong values`() { | ||
| val result = USat(0uL) - USat(ULong.MAX_VALUE) | ||
| assertEquals(0uL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `chained minus operations work correctly`() { | ||
| val intermediate = 100uL.safe() - 30uL.safe() | ||
| val result = intermediate.safe() - 20uL.safe() | ||
| assertEquals(50uL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `chained minus returns zero when intermediate would underflow`() { | ||
| val intermediate = 10uL.safe() - 20uL.safe() | ||
| val result = intermediate.safe() - 5uL.safe() | ||
| assertEquals(0uL, result) | ||
| } | ||
| // endregion | ||
|
|
||
| // region Addition | ||
| @Test | ||
| fun `plus returns sum`() { | ||
| val result = USat(10uL) + USat(5uL) | ||
| assertEquals(15uL, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `plus saturates at max when would overflow`() { | ||
| val result = USat(ULong.MAX_VALUE) + USat(1uL) | ||
| assertEquals(ULong.MAX_VALUE, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `plus saturates when both values are large`() { | ||
| val result = USat(ULong.MAX_VALUE - 10uL) + USat(20uL) | ||
| assertEquals(ULong.MAX_VALUE, result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `chained plus operations work correctly`() { | ||
| val intermediate = 10uL.safe() + 20uL.safe() | ||
| val result = intermediate.safe() + 30uL.safe() | ||
| assertEquals(60uL, result) | ||
| } | ||
| // endregion | ||
|
|
||
| // region Comparisons | ||
| @Test | ||
| fun `compareTo returns negative when less than`() { | ||
| assertTrue(USat(5uL) < USat(10uL)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `compareTo returns positive when greater than`() { | ||
| assertTrue(USat(10uL) > USat(5uL)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `compareTo returns zero when equal`() { | ||
| assertEquals(0, USat(10uL).compareTo(USat(10uL))) | ||
| } | ||
|
|
||
| @Test | ||
| fun `comparison operators work correctly`() { | ||
| assertTrue(USat(5uL) <= USat(10uL)) | ||
| assertTrue(USat(10uL) >= USat(5uL)) | ||
| assertTrue(USat(10uL) <= USat(10uL)) | ||
| assertTrue(USat(10uL) >= USat(10uL)) | ||
| assertFalse(USat(10uL) < USat(10uL)) | ||
| assertFalse(USat(10uL) > USat(10uL)) | ||
| } | ||
| // endregion | ||
|
|
||
| // region Realistic scenarios | ||
| @Test | ||
| fun `realistic bitcoin calculation`() { | ||
| val channelSize = 10_000_000uL // 0.1 BTC in sats | ||
| val balance = 1_000_000uL // 0.01 BTC in sats | ||
|
|
||
| val maxSend = channelSize.safe() - balance.safe() | ||
|
|
||
| assertEquals(9_000_000uL, maxSend) | ||
| } | ||
|
|
||
| @Test | ||
| fun `minus prevents the coerceAtLeast bug`() { | ||
| // The old pattern: (5u - 10u).coerceAtLeast(0u) | ||
| // Would incorrectly return ULong.MAX_VALUE - 4 | ||
| val old = (5uL - 10uL).coerceAtLeast(0u) | ||
| assertTrue(old > 1000000u) // WRONG! Shows the bug | ||
|
|
||
| // The new pattern: 5u.safe() - 10u.safe() | ||
| val new = 5uL.safe() - 10uL.safe() | ||
| assertEquals(0uL, new) // CORRECT! | ||
| } | ||
| // endregion | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.