carlos / platform Public

console: refuse a euro conversion that is not a finite int64

eurCents converted the rounded quotient straight to int64, so a tiny
positive rate or a huge amount returned a wrapped number with true. It
now returns (0, false) when the quotient is NaN, infinite or outside
int64.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev bd66229d60dd2228f76f043240b767ac4febd940 parent f594ad3
2 files changed, +21 −2
  • internal/console/billingrates.go +8 −2
  • internal/console/billingrates_test.go +13 −0
diff --git a/internal/console/billingrates.go b/internal/console/billingrates.go
index 94a53b5a6..30c0fdcef 100644
--- a/internal/console/billingrates.go
+++ b/internal/console/billingrates.go
@@ -27,7 +27,8 @@ type eurRates struct {
// eurCents converts minor units of currency to euro cents: euros are
// themselves, another currency is divided by its rate, rounded. Every
// selling currency has two decimals, so minor units convert to cents
-// directly. False when the currency has no rate.
+// directly. False when the currency has no rate, or the result is not a
+// finite number within int64.
func (r eurRates) eurCents(minor int64, currency string) (int64, bool) {
if currency == "eur" {
return minor, true
@@ -36,7 +37,12 @@ func (r eurRates) eurCents(minor int64, currency string) (int64, bool) {
if !ok || !(rate > 0) || math.IsInf(rate, 0) {
return 0, false
}
- return int64(math.Round(float64(minor) / rate)), true
+ // float64(math.MaxInt64) is 2^63, itself out of range; -2^63 is in.
+ q := math.Round(float64(minor) / rate)
+ if math.IsNaN(q) || q >= float64(math.MaxInt64) || q < float64(math.MinInt64) {
+ return 0, false
+ }
+ return int64(q), true
}
// PutEURRates stores a day's rates, one row per currency; a refetch of the
diff --git a/internal/console/billingrates_test.go b/internal/console/billingrates_test.go
index f4e834e82..b18d261e7 100644
--- a/internal/console/billingrates_test.go
+++ b/internal/console/billingrates_test.go
@@ -3,6 +3,7 @@ package console
import (
"context"
"errors"
+ "math"
"sync"
"testing"
"time"
@@ -46,6 +47,18 @@ func TestEURCents(t *testing.T) {
if got, ok := r.eurCents(2400, "chf"); ok {
t.Errorf("eurCents(2400, chf) = %d, true; want false", got)
}
+ // A quotient that is infinite or past int64 is no number, not a wrapped one.
+ r.PerEUR["xts"] = math.SmallestNonzeroFloat64
+ if got, ok := r.eurCents(2800, "xts"); ok {
+ t.Errorf("eurCents(2800) at a tiny rate = %d, true; want false", got)
+ }
+ r.PerEUR["xxx"] = 0.5
+ if got, ok := r.eurCents(math.MaxInt64, "xxx"); ok {
+ t.Errorf("eurCents(MaxInt64) at 0.5 = %d, true; want false", got)
+ }
+ if got, ok := r.eurCents(math.MinInt64, "xxx"); ok {
+ t.Errorf("eurCents(MinInt64) at 0.5 = %d, true; want false", got)
+ }
}
func TestRatesPassFetchesDaily(t *testing.T) {