Skip to content

Commit 6d7f801

Browse files
authored
backends: Fix crash when interrupting during interactive prompt for values' (#36448)
* backends: Convert null values to empty objects before coercion * backends: add test case for interrupt crash * Add changelog
1 parent 2ce5fca commit 6d7f801

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'backends: Fix crash when interrupting during interactive prompt for values'
3+
time: 2025-02-06T14:52:17.033964+01:00
4+
custom:
5+
Issue: "36448"

internal/backend/backendbase/base.go

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ func (b Base) ConfigSchema() *configschema.Block {
5757
func (b Base) PrepareConfig(configVal cty.Value) (cty.Value, tfdiags.Diagnostics) {
5858
var diags tfdiags.Diagnostics
5959

60+
if configVal.IsNull() {
61+
// We expect the backend configuration to be an object, so if it's
62+
// null for some reason (e.g. because of an interrupt), we'll turn
63+
// it into an empty object so that we can still coerce it
64+
configVal = cty.EmptyObjectVal
65+
}
66+
6067
schema := b.Schema
6168

6269
v, err := schema.CoerceValue(configVal)

internal/backend/backendbase/base_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,41 @@ func TestBase_deprecatedArg(t *testing.T) {
214214
}
215215
})
216216
}
217+
218+
func TestBase_nullCrash(t *testing.T) {
219+
// This test ensures that we don't crash while applying defaults to
220+
// a null value
221+
222+
b := Base{
223+
Schema: &configschema.Block{
224+
Attributes: map[string]*configschema.Attribute{
225+
"foo": {
226+
Type: cty.String,
227+
Required: true,
228+
},
229+
},
230+
},
231+
SDKLikeDefaults: SDKLikeDefaults{
232+
"foo": {
233+
Fallback: "fallback",
234+
},
235+
},
236+
}
237+
238+
t.Run("error", func(t *testing.T) {
239+
// We pass an explicit null value here to simulate an interrupt
240+
_, gotDiags := b.PrepareConfig(cty.NullVal(cty.Object(map[string]cty.Type{
241+
"foo": cty.String,
242+
})))
243+
var wantDiags tfdiags.Diagnostics
244+
wantDiags = wantDiags.Append(
245+
&hcl.Diagnostic{
246+
Severity: hcl.DiagError,
247+
Summary: "Invalid backend configuration",
248+
Detail: "The backend configuration is incorrect: attribute \"foo\" is required.",
249+
})
250+
if diff := cmp.Diff(wantDiags.ForRPC(), gotDiags.ForRPC()); diff != "" {
251+
t.Errorf("wrong diagnostics\n%s", diff)
252+
}
253+
})
254+
}

0 commit comments

Comments
 (0)