Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens price utilities against invalid inputs that could previously lead to uint wraparound (from negative signed ints) or panics (e.g., big.NewRat with a zero denominator), adding explicit validation and error paths.
Changes:
- Add
xdr.Price.Validate()and guardString,Equal,Cheaper,Normalize, andInvertagainst invalid prices. - Reject negative inputs in
price.MulFractionRoundDownandprice.mulFractionRoundUpvia a newErrNoNegativeserror.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| xdr/price.go | Adds price validation and uses it to prevent panics/wraparound in Price helpers. |
| price/main.go | Adds a negative-input error and enforces non-negative operands in fraction multiplication helpers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Below are specific comments with actionable suggestions and evidence from the Go standard library. 1. Remove
|
…rted old price utils
What
In
xdr/prices.go::Price.Cheaper(),prices/main.go::MulFractionRoundDown()andprices/main.go::mulFractionRoundUp(),Int32andInt64values are cast touint64values, which would wrap to large, meaningless values. Then, there are severalbig.Rat()calls where the denominator is checked to not be zero, which would cause an unrecoverable panic.For
xdr/prices.go, because fixing these would cause breaking changes, this adds equivalentTry*()methods that implement their corresponding methods with errors returned instead of panicking/returning invalid output. The new methods are unit tested as the old ones are, along with a new test to ensure invalid output is rejected.The old methods are marked as deprecated, except for Price.String() as this implements the Stringer interface -- this case is still also changed to be handled without panic.
Why
This adds defensive programming methods for developers utilizing these functions, but note that functions are already called safely in Stellar services.
Known limitations
N/A