Skip to content

Commit 61de0c4

Browse files
committed
fix: Prevent panic when merging IPAM config
Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
1 parent 53f73f4 commit 61de0c4

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

override/merge.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,13 @@ func mergeUlimit(_ any, o any, p tree.Path) (any, error) {
226226
}
227227

228228
func mergeIPAMConfig(c any, o any, path tree.Path) (any, error) {
229+
if _, ok := c.([]any); !ok {
230+
return nil, fmt.Errorf("cannot merge IPAM config at %s: base value is not a list", path)
231+
}
232+
if _, ok := o.([]any); !ok {
233+
return nil, fmt.Errorf("cannot merge IPAM config at %s: override value is not a list", path)
234+
}
235+
229236
var ipamConfigs []any
230237
for _, original := range c.([]any) {
231238
right := convertIntoMapping(original, nil)

override/merge_networks_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package override
1818

1919
import (
20+
"strings"
2021
"testing"
22+
23+
"github.com/compose-spec/compose-go/v2/tree"
2124
)
2225

2326
func Test_mergeYamlServiceNetworkSequence(t *testing.T) {
@@ -226,3 +229,40 @@ networks:
226229
network2:
227230
`)
228231
}
232+
233+
func Test_mergeIPAMConfig_invalidTypes(t *testing.T) {
234+
tests := []struct {
235+
name string
236+
c any
237+
o any
238+
wantErr string
239+
}{
240+
{
241+
name: "c is not a list",
242+
c: map[string]any{},
243+
o: []any{},
244+
wantErr: "base value is not a list",
245+
},
246+
{
247+
name: "o is not a list",
248+
c: []any{},
249+
o: map[string]any{},
250+
wantErr: "override value is not a list",
251+
},
252+
{
253+
name: "both are not lists",
254+
c: "invalid",
255+
o: 123,
256+
wantErr: "base value is not a list",
257+
},
258+
}
259+
260+
for _, tt := range tests {
261+
t.Run(tt.name, func(t *testing.T) {
262+
_, err := mergeIPAMConfig(tt.c, tt.o, tree.NewPath("networks", "ipam", "config"))
263+
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
264+
t.Fatalf("expected error containing %q, got %v", tt.wantErr, err)
265+
}
266+
})
267+
}
268+
}

0 commit comments

Comments
 (0)