fix(mcp_impl): validate JSON amounts as exact integers before big.Int conversion#143
fix(mcp_impl): validate JSON amounts as exact integers before big.Int conversion#143gfyrag wants to merge 1 commit into
Conversation
… conversion
`parseBalancesJson` reads each balance amount as `float64` (Go's JSON
default for `any`) and previously fed it straight into
`big.NewFloat(amount).Int(new(big.Int))`, which silently truncated
fractional parts and rounded values past float64's exact-integer range
(±(2^53 - 1)). For a financial DSL, both are silent data corruption.
Add a guard before the conversion:
- Reject magnitudes outside [-(2^53-1), 2^53-1] — beyond this range
float64 cannot uniquely represent every integer, so the value the
caller meant has already been lost in JSON decoding.
- Reject fractional values (`amount != float64(int64(amount))`).
- Use `big.NewInt(int64(amount))` directly since the validation has
proven the float represents an exact int64.
The error message names the offending account/asset so the caller
knows which balance to fix.
This flips the failing regression tests in the stacked parent PR from
red to green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review (1)
Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## test/mcp-float-truncation-bug #143 +/- ##
================================================================
Coverage ? 67.21%
================================================================
Files ? 47
Lines ? 5069
Branches ? 0
================================================================
Hits ? 3407
Misses ? 1462
Partials ? 200 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
Validates JSON amounts before converting to
*big.Intininternal/mcp_impl/handlers.go::parseBalancesJson. Stacked on #142(the failing regression test); merging this flips the test green.
JSON numeric values reach the handler as
float64(Go's default). Theprevious code used
big.NewFloat(amount).Int(new(big.Int)), which:100.9→100)±(2^53 - 1)(already lost in JSONdecoding before reaching the handler)
The fix introduces a single guard before the conversion:
where
maxExactJSONInt = 9_007_199_254_740_991(2^53 - 1). The errormessage names the offending
account/assetso callers can pinpoint thebad balance.
Stack
mainTest plan
TestParseBalancesJsonRejectsNonIntegerAmountspasses (red on test(mcp_impl): pin parseBalancesJson float64 truncation bug (failing) #142, green here).TestParseBalancesJsonRejectsUnsafelyLargeAmountspasses (red on test(mcp_impl): pin parseBalancesJson float64 truncation bug (failing) #142, green here).TestParseBalancesJsonAcceptsExactIntegerAmountsstill passes.just pre-commitclean.🤖 Generated with Claude Code