hashicorp/packer · error
can't compute sum of opposing infinities
Error message
can't compute sum of opposing infinities
What it means
The `sum()` HCL2 function recovers a big.Float ErrNaN panic caused by adding +Inf and -Inf (or Inf - Inf), which is undefined math, and converts it into this regular error to stay inside the cty Function abstraction.
Source
Thrown at hcl2template/function/sum.go:52
arg := args[0].AsValueSlice()
ty := args[0].Type()
if !ty.IsListType() && !ty.IsSetType() && !ty.IsTupleType() {
return cty.NilVal, function.NewArgErrorf(0, "argument must be list, set, or tuple. Received %s", ty.FriendlyName())
}
if !args[0].IsWhollyKnown() {
return cty.UnknownVal(cty.Number), nil
}
// big.Float.Add can panic if the input values are opposing infinities,
// so we must catch that here in order to remain within
// the cty Function abstraction.
defer func() {
if r := recover(); r != nil {
if _, ok := r.(big.ErrNaN); ok {
ret = cty.NilVal
err = fmt.Errorf("can't compute sum of opposing infinities")
} else {
// not a panic we recognize
panic(r)
}
}
}()
s := arg[0]
if s.IsNull() {
return cty.NilVal, function.NewArgErrorf(0, "argument must be list, set, or tuple of number values")
}
s, err = convert.Convert(s, cty.Number)
if err != nil {
return cty.NilVal, function.NewArgErrorf(0, "argument must be list, set, or tuple of number values")
}
for _, v := range arg[1:] {
if v.IsNull() {
return cty.NilVal, function.NewArgErrorf(0, "argument must be list, set, or tuple of number values")View on GitHub (pinned to eb36e3c3e4)
Solutions
- Remove or fix the offending values so +Inf and -Inf are not both present
- Sanitize the list before summing: filter or replace infinities with finite bounds
- Check upstream math (divisions by zero) that produce infinities
- Example: use `can()`/`try()` to drop non-finite elements
Example fix
// before total = sum([1, pow(0, -1), -pow(0, -1)]) // +Inf and -Inf -> error // after nums = [for n in raw_nums : n if !isnan(n) && !isinfinite(n)] total = sum(nums)
Defensive patterns
Strategy: validation
Validate before calling
// filter non-finite values before summing
locals {
finite = [for n in var.nums : n if n != pow(0, -1) && n != -pow(0, -1)]
total = sum(local.finite)
} Try / catch
// tolerate and fall back
locals {
total = try(sum(var.nums), 0)
} Prevention
- Check divisions/denominators that could yield +Inf or -Inf
- Sanitize number lists from computed data sources before sum()
- Use try() around sum() when inputs are untrusted
When it happens
Trigger: `sum([...])` over a list of numbers containing both +Inf and -Inf (e.g. numbers constructed via `pow(0, -1)`-style infinities or from computed data).
Common situations: Building number lists from division expressions that can yield infinities; passing computed floats from data sources whose math produced Inf on both signs.
Related errors
- too many values, 1 needed: %v
- failed to write gzip raw data: %w
- failed to flush gzip writer: %w
- failed to close gzip writer: %w
- failed to read file %q: %s
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/2b308c659c08e8cf.
Report an issue: GitHub.