Skip to content

Commit 76a6844

Browse files
committed
Fix panic when the single interpolation result is null
1 parent 5c140ce commit 76a6844

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

hclsyntax/expression_template.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,21 @@ func (e *TemplateWrapExpr) walkChildNodes(w internalWalkFunc) {
250250
}
251251

252252
func (e *TemplateWrapExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
253-
return e.Wrapped.Value(ctx)
253+
val, diags := e.Wrapped.Value(ctx)
254+
255+
// If the single interpolation result is null, we need to return the same
256+
// error as we would for a TemplateExpr with a null part.
257+
if val.IsNull() {
258+
diags = append(diags, &hcl.Diagnostic{
259+
Severity: hcl.DiagError,
260+
Summary: "Invalid template interpolation value",
261+
Detail: "The expression result is null. Cannot include a null value in a string template.",
262+
Subject: e.Range().Ptr(),
263+
Expression: e.Wrapped,
264+
EvalContext: ctx,
265+
})
266+
}
267+
return val, diags
254268
}
255269

256270
func (e *TemplateWrapExpr) Range() hcl.Range {

hclsyntax/expression_template_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,26 @@ trim`,
403403
cty.UnknownVal(cty.String).Mark("sensitive").Refine().NotNull().StringPrefixFull("test_").NewValue(),
404404
0,
405405
},
406+
{ // template with a single wrapped expr that is null
407+
`${hello}`,
408+
&hcl.EvalContext{
409+
Variables: map[string]cty.Value{
410+
"hello": cty.NullVal(cty.String),
411+
},
412+
},
413+
cty.NullVal(cty.String),
414+
1, // error about null value
415+
},
416+
{
417+
`foo ${hello}`,
418+
&hcl.EvalContext{
419+
Variables: map[string]cty.Value{
420+
"hello": cty.NullVal(cty.String),
421+
},
422+
},
423+
cty.StringVal("foo "),
424+
1, // error about null value
425+
},
406426
}
407427

408428
for _, test := range tests {

0 commit comments

Comments
 (0)