billing: SettledShare exact with no intermediate overflow
part x settled overflowed int64 even when the share fit (4e9 of 8e9 settled 8e9 gave -611686018), and the rounding step could overflow at MinInt64. Compute in math/big with the same round-half-away-from-zero result; a result beyond int64 saturates. Boundary cases added.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+31
−17
internal/billing/payments.go+21 −17internal/billing/payments_test.go+10 −0
diff --git a/internal/billing/payments.go b/internal/billing/payments.go| index ce0073756..d7098112f 100644 |
| --- a/internal/billing/payments.go |
| +++ b/internal/billing/payments.go |
| @@ -1,6 +1,10 @@ |
| package billing |
| -import "time" |
| +import ( |
| + "math" |
| + "math/big" |
| + "time" |
| +) |
| // MovementKind is what a balance movement is, in neutral words. |
| type MovementKind string |
| @@ -82,28 +86,28 @@ type ChargeRecord struct { |
| // SettledShare is round(part × settled / whole), rounding halves away from |
| // zero, and 0 when whole is 0: the settled tax of a charge, or a refund's |
| -// share of it. The sign follows settled. |
| +// share of it. The sign follows settled. The arithmetic is exact (no |
| +// intermediate overflow); a result beyond int64, which a share no larger |
| +// than its whole never is, saturates at the nearest bound. |
| func SettledShare(part, whole, settled int64) int64 { |
| if whole == 0 { |
| return 0 |
| } |
| - n := part * settled |
| - q, r := n/whole, n%whole |
| - if 2*abs64(r) >= abs64(whole) { |
| - if (n < 0) != (whole < 0) { |
| - q-- |
| - } else { |
| - q++ |
| - } |
| + n := new(big.Int).Mul(big.NewInt(part), big.NewInt(settled)) |
| + w := big.NewInt(whole) |
| + q, r := new(big.Int).QuoRem(n, w, new(big.Int)) |
| + twiceR := new(big.Int).Lsh(new(big.Int).Abs(r), 1) |
| + if twiceR.Cmp(new(big.Int).Abs(w)) >= 0 { |
| + q.Add(q, big.NewInt(int64(n.Sign()*w.Sign()))) |
| } |
| - return q |
| -} |
| - |
| -func abs64(v int64) int64 { |
| - if v < 0 { |
| - return -v |
| + switch { |
| + case q.IsInt64(): |
| + return q.Int64() |
| + case q.Sign() > 0: |
| + return math.MaxInt64 |
| + default: |
| + return math.MinInt64 |
| } |
| - return v |
| } |
| // FeePayment records a fee movement: the provider charging the balance |
diff --git a/internal/billing/payments_test.go b/internal/billing/payments_test.go| index f1f0f2f66..5c5b34aee 100644 |
| --- a/internal/billing/payments_test.go |
| +++ b/internal/billing/payments_test.go |
| @@ -1,6 +1,7 @@ |
| package billing |
| import ( |
| + "math" |
| "testing" |
| "time" |
| ) |
| @@ -17,6 +18,15 @@ func TestSettledShare(t *testing.T) { |
| {"exact", 500, 1000, 300, 150}, |
| {"half rounds away from zero", 1, 2, 1, 1}, |
| {"negative half rounds away from zero", 1, 2, -1, -1}, |
| + {"product beyond int64, answer within", 4000000000, 8000000000, 8000000000, 4000000000}, |
| + {"product beyond int64, negative", 4000000000, 8000000000, -8000000000, -4000000000}, |
| + {"half of the largest settled", 1, 2, math.MaxInt64, 4611686018427387904}, |
| + {"half of the smallest settled", 1, 2, math.MinInt64, -4611686018427387904}, |
| + {"all of the smallest", math.MinInt64, math.MinInt64, math.MinInt64, math.MinInt64}, |
| + {"whole at the smallest", 1, math.MinInt64, math.MinInt64, 1}, |
| + {"remainder near the bound", math.MaxInt64 - 1, math.MaxInt64, math.MaxInt64, math.MaxInt64 - 1}, |
| + {"beyond int64 saturates", math.MaxInt64, 1, 2, math.MaxInt64}, |
| + {"below int64 saturates", math.MaxInt64, 1, -2, math.MinInt64}, |
| } |
| for _, c := range cases { |
| if got := SettledShare(c.part, c.whole, c.settled); got != c.want { |