diff --git a/.changelog/25294.txt b/.changelog/25294.txt new file mode 100644 index 000000000..33ab42288 --- /dev/null +++ b/.changelog/25294.txt @@ -0,0 +1,3 @@ +```release-note:bug +hcl: Avoid panics by checking null values on durations +``` diff --git a/jobspec2/hcl_conversions.go b/jobspec2/hcl_conversions.go index ecf5e4714..443afe288 100644 --- a/jobspec2/hcl_conversions.go +++ b/jobspec2/hcl_conversions.go @@ -42,9 +42,20 @@ func newHCLDecoder() *gohcl.Decoder { func decodeDuration(expr hcl.Expression, ctx *hcl.EvalContext, val interface{}) hcl.Diagnostics { srcVal, diags := expr.Value(ctx) + if srcVal.IsNull() { + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Unsuitable value", + Detail: "Unsuitable duration value: nil", + Subject: expr.StartRange().Ptr(), + Context: expr.Range().Ptr(), + }) + return diags + } if srcVal.Type() == cty.String { dur, err := time.ParseDuration(srcVal.AsString()) + if err != nil { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, @@ -68,7 +79,6 @@ func decodeDuration(expr hcl.Expression, ctx *hcl.EvalContext, val interface{}) Context: expr.Range().Ptr(), }) return diags - } err := gocty.FromCtyValue(srcVal, val) @@ -81,7 +91,6 @@ func decodeDuration(expr hcl.Expression, ctx *hcl.EvalContext, val interface{}) Context: expr.Range().Ptr(), }) } - return diags }