{"record":{"id":"2b308c659c08e8cf","repo":"hashicorp/packer","slug":"can-t-compute-sum-of-opposing-infinities","errorCode":null,"errorMessage":"can't compute sum of opposing infinities","messagePattern":"can't compute sum of opposing infinities","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/function/sum.go","lineNumber":52,"sourceCode":"\t\targ := args[0].AsValueSlice()\n\t\tty := args[0].Type()\n\n\t\tif !ty.IsListType() && !ty.IsSetType() && !ty.IsTupleType() {\n\t\t\treturn cty.NilVal, function.NewArgErrorf(0, \"argument must be list, set, or tuple. Received %s\", ty.FriendlyName())\n\t\t}\n\n\t\tif !args[0].IsWhollyKnown() {\n\t\t\treturn cty.UnknownVal(cty.Number), nil\n\t\t}\n\n\t\t// big.Float.Add can panic if the input values are opposing infinities,\n\t\t// so we must catch that here in order to remain within\n\t\t// the cty Function abstraction.\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tif _, ok := r.(big.ErrNaN); ok {\n\t\t\t\t\tret = cty.NilVal\n\t\t\t\t\terr = fmt.Errorf(\"can't compute sum of opposing infinities\")\n\t\t\t\t} else {\n\t\t\t\t\t// not a panic we recognize\n\t\t\t\t\tpanic(r)\n\t\t\t\t}\n\t\t\t}\n\t\t}()\n\n\t\ts := arg[0]\n\t\tif s.IsNull() {\n\t\t\treturn cty.NilVal, function.NewArgErrorf(0, \"argument must be list, set, or tuple of number values\")\n\t\t}\n\t\ts, err = convert.Convert(s, cty.Number)\n\t\tif err != nil {\n\t\t\treturn cty.NilVal, function.NewArgErrorf(0, \"argument must be list, set, or tuple of number values\")\n\t\t}\n\t\tfor _, v := range arg[1:] {\n\t\t\tif v.IsNull() {\n\t\t\t\treturn cty.NilVal, function.NewArgErrorf(0, \"argument must be list, set, or tuple of number values\")","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/function/sum.go#L34-L70","documentation":"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.","triggerScenarios":"`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).","commonSituations":"Building number lists from division expressions that can yield infinities; passing computed floats from data sources whose math produced Inf on both signs.","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"],"exampleFix":"// before\ntotal = sum([1, pow(0, -1), -pow(0, -1)])  // +Inf and -Inf -> error\n// after\nnums = [for n in raw_nums : n if !isnan(n) && !isinfinite(n)]\ntotal = sum(nums)","handlingStrategy":"validation","validationCode":"// filter non-finite values before summing\nlocals {\n  finite = [for n in var.nums : n if n != pow(0, -1) && n != -pow(0, -1)]\n  total  = sum(local.finite)\n}","typeGuard":null,"tryCatchPattern":"// tolerate and fall back\nlocals {\n  total = try(sum(var.nums), 0)\n}","preventionTips":["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"],"tags":["hcl2","function","sum","math"],"backgroundTag":"indeterminate-math-operation","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}