fix(api): keep exact quantity products the int64 coefficient cannot hold

resource.Quantity.Mul returns false when the product no longer fits an
int64 coefficient, not when it is wrong: it carries on in arbitrary
precision. The evaluator read that as an overflow and failed with
"quantity 20992Mi * 3 overflows" for any fractional binary quantity
above about 8.5Gi, since such a quantity is stored at a scale whose
coefficient leaves int64 long before its value does. Declared sizes
like 20.5Gi are ordinary, so every pool or disk using one could not be
reserved.

Tested with fractional Gi and Ti quantities scaled directly and after a
sum.

Fixes: 89aba6e104 ("feat(api): add the reservation evaluator for tenant quotas")
Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
This commit is contained in:
Mattia Eleuteri committed 2026-11-18 15:58:55 +01:00
1 parent a9245ec165
commit 6f705fe0e9
2 files changed
+33 -3

No files matched your search

+5 -3
View File
@@ -285,9 +285,11 @@ func mulQuantity(q, n ref.Val) ref.Val {
return types.MaybeNoSuchOverloadErr(n)
}
out := qq.DeepCopy()
if !out.Mul(int64(nn)) {
return types.NewErr("quantity %s * %d overflows", qq.String(), int64(nn))
}
// Mul reports whether the product still fits an int64 coefficient, not
// whether it is exact: past that it carries on in arbitrary precision,
// which a fractional binary quantity such as 20.5Gi reaches at any size
// worth reserving.
out.Mul(int64(nn))
return apiservercel.Quantity{Quantity: &out}
}
+28
View File
@@ -467,6 +467,34 @@ func TestEvaluateQuantityArithmetic(t *testing.T) {
}
}
// A fractional binary quantity is stored at a scale whose coefficient leaves
// int64 well before its value does, which is where resource.Quantity.Mul
// reports an inexact int64 for a product it still holds exactly.
func TestEvaluateMulKeepsExactProducts(t *testing.T) {
env := testEnvironment(t)
cases := map[string]struct {
expr string
want string
}{
"fractional Gi": {`quantity('20.5Gi').mul(3)`, "62976Mi"},
"fractional Ti": {`quantity('1.5Ti').mul(3)`, "4608Gi"},
"after a sum": {`quantity('8.5Gi').add(quantity('1Gi')).mul(2)`, "19Gi"},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
ev, err := Compile(&Declaration{Resources: map[string]string{"x": tc.expr}}, nil)
if err != nil {
t.Fatal(err)
}
got, err := ev.Evaluate(map[string]any{}, env)
if err != nil {
t.Fatal(err)
}
assertList(t, name, got, rl("x", tc.want))
})
}
}
func TestEvaluateIsConcurrencySafe(t *testing.T) {
env := testEnvironment(t)
vm := mustCompile(t, vmInstanceDecl, "vm-instance")