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.
22 lines
599 B
Go
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)
|
|
}
|