Skip to content

Commit f4ea122

Browse files
committed
prevent segfault in GetRawConfigForKey when list is empty
1 parent eae5af4 commit f4ea122

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

internal/meta/extractors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ func getKeyInRawConfigMap(rawConfig map[string]cty.Value, key string, ty cty.Typ
137137
case value.Type().IsListType():
138138
// If it's a list and the second element of the key is an index, we look for the value in the list at the given index
139139
if index, err := strconv.Atoi(keys[1]); err == nil {
140+
if value.Length().Equals(cty.NumberIntVal(0)).True() {
141+
// If the list is empty, then the value is not set
142+
return nil, false
143+
}
144+
140145
return getKeyInRawConfigMap(value.AsValueSlice()[index].AsValueMap(), strings.Join(keys[2:], ""), ty)
141146
}
142147
// If it's a list and the second element of the key is '#', we look for the value in the list's first element

internal/services/instance/server.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -514,18 +514,13 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
514514

515515
d.SetId(zonal.NewID(zone, res.Server.ID).String())
516516

517-
err = renameRootVolumeIfNeeded(d, api, zone, res.Server.Volumes)
518-
if err != nil {
519-
return diag.FromErr(err)
520-
}
521-
522517
_, err = waitForServer(ctx, api.API, zone, res.Server.ID, d.Timeout(schema.TimeoutCreate))
523518
if err != nil {
524519
return diag.FromErr(err)
525520
}
526521

527522
////
528-
// Configure Block Volume
523+
// Configure Volumes
529524
////
530525
var diags diag.Diagnostics
531526

@@ -536,6 +531,11 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
536531
}
537532
}
538533

534+
err = renameRootVolumeIfNeeded(d, api, zone, res.Server.Volumes)
535+
if err != nil {
536+
return diag.FromErr(err)
537+
}
538+
539539
////
540540
// Set user data
541541
////
@@ -768,10 +768,6 @@ func ResourceInstanceServerRead(ctx context.Context, d *schema.ResourceData, m a
768768
_ = d.Set("root_volume", []map[string]any{rootVolume})
769769
_ = d.Set("additional_volume_ids", additionalVolumesIDs)
770770

771-
if len(additionalVolumes) > 0 {
772-
_ = d.Set("additional_volumes", additionalVolumes)
773-
}
774-
775771
////
776772
// Read server user data
777773
////
@@ -1706,8 +1702,12 @@ func GetEndOfServiceDate(ctx context.Context, client *scw.Client, zone scw.Zone,
17061702
}
17071703

17081704
func renameRootVolumeIfNeeded(d *schema.ResourceData, api *instancehelpers.BlockAndInstanceAPI, zone scw.Zone, volumes map[string]*instanceSDK.VolumeServer) error {
1709-
if rootVolumeName, setbyUser := meta.GetRawConfigForKey(d, "root_volume.0.name", cty.String); setbyUser {
1710-
if volumes["0"].Name != nil && *volumes["0"].Name != rootVolumeName {
1705+
if volumes == nil || volumes["0"] == nil || volumes["0"].Name != nil {
1706+
return nil
1707+
}
1708+
1709+
if rootVolumeName, setByUser := meta.GetRawConfigForKey(d, "root_volume.0.name", cty.String); setByUser {
1710+
if *volumes["0"].Name != rootVolumeName {
17111711
_, err := api.UpdateVolume(&instanceSDK.UpdateVolumeRequest{
17121712
Zone: zone,
17131713
VolumeID: volumes["0"].ID,

0 commit comments

Comments
 (0)