Files
new-api/common/quota_math.go
T
CaIon d0bd8aac74 fix(billing): validate quantity parameters and harden quota calculations
Bound user-supplied count/duration parameters at request validation,
route ratio multipliers through guarded setters, and use saturating
int conversions in all quota math paths.
2026-07-07 00:21:06 +08:00

22 lines
599 B
Go

package common
import "math"
// QuotaFromFloat converts a computed quota value to int with saturation.
// Quota products can include user-controlled multipliers (image n, video
// seconds, resolution ratios); an oversized product must never wrap around
// and turn a charge into a credit. The bound is int32 because quota columns
// (user/token/log) are 32-bit integers in the database.
func QuotaFromFloat(value float64) int {
if math.IsNaN(value) {
return 0
}
if value >= math.MaxInt32 {
return math.MaxInt32
}
if value <= math.MinInt32 {
return math.MinInt32
}
return int(value)
}