Skip to content

fix: Prevent panic when merging IPAM config #766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions override/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ func mergeUlimit(_ any, o any, p tree.Path) (any, error) {
}

func mergeIPAMConfig(c any, o any, path tree.Path) (any, error) {
if _, ok := c.([]any); !ok {
return nil, fmt.Errorf("cannot merge IPAM config at %s: base value is not a list", path)
}
if _, ok := o.([]any); !ok {
return nil, fmt.Errorf("cannot merge IPAM config at %s: override value is not a list", path)
}

var ipamConfigs []any
configs, ok := c.([]any)
if !ok {
Expand Down
40 changes: 40 additions & 0 deletions override/merge_networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package override

import (
"strings"
"testing"

"github.com/compose-spec/compose-go/v2/tree"
)

func Test_mergeYamlServiceNetworkSequence(t *testing.T) {
Expand Down Expand Up @@ -226,3 +229,40 @@ networks:
network2:
`)
}

func Test_mergeIPAMConfig_invalidTypes(t *testing.T) {
tests := []struct {
name string
c any
o any
wantErr string
}{
{
name: "c is not a list",
c: map[string]any{},
o: []any{},
wantErr: "base value is not a list",
},
{
name: "o is not a list",
c: []any{},
o: map[string]any{},
wantErr: "override value is not a list",
},
{
name: "both are not lists",
c: "invalid",
o: 123,
wantErr: "base value is not a list",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := mergeIPAMConfig(tt.c, tt.o, tree.NewPath("networks", "ipam", "config"))
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("expected error containing %q, got %v", tt.wantErr, err)
}
})
}
}