From: Simon Ruderich Date: Wed, 4 Feb 2026 17:29:29 +0000 (+0100) Subject: config: fix data race in template's GroupHosts X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=a36b5eb49a2f995f1ddab3e0654e25609687dcdf;p=safcm%2Fsafcm.git config: fix data race in template's GroupHosts ================== WARNING: DATA RACE Read at 0x00c00015a5a0 by goroutine 25: ruderich.org/simon/safcm/cmd/safcm/config.ResolveHostGroups.func1() cmd/safcm/config/groups.go:130 +0x1c7 ruderich.org/simon/safcm/cmd/safcm/config.ResolveHostGroups() cmd/safcm/config/groups.go:154 +0x521 [...] Previous write at 0x00c00015a5a0 by goroutine 21: slices.insertionSortOrdered[go.shape.string]() /usr/lib/go-1.24/src/slices/zsortordered.go:15 +0x2f9 slices.pdqsortOrdered[go.shape.string]() /usr/lib/go-1.24/src/slices/zsortordered.go:75 +0x6c4 slices.Sort[go.shape.[]string,go.shape.string]() /usr/lib/go-1.24/src/slices/sort.go:18 +0xd6 ruderich.org/simon/safcm/cmd/safcm/config.(*templateArgs).GroupHosts() cmd/safcm/config/templates.go:116 +0x97 [...] --- diff --git a/cmd/safcm/config/groups.go b/cmd/safcm/config/groups.go index 626c053..4a9b365 100644 --- a/cmd/safcm/config/groups.go +++ b/cmd/safcm/config/groups.go @@ -9,6 +9,7 @@ import ( "fmt" "os" "regexp" + "slices" "sort" "strings" @@ -84,6 +85,9 @@ func LoadGroups(cfg *Config, hosts *Hosts) (map[string][]string, error) { } return nil, fmt.Errorf("%s member %q not found", errPrefix, x) } + + // Keep sorted for templates + slices.Sort(groups[name]) } // Sanity check for global configuration diff --git a/cmd/safcm/config/groups_test.go b/cmd/safcm/config/groups_test.go index bba47d9..d0cb9b4 100644 --- a/cmd/safcm/config/groups_test.go +++ b/cmd/safcm/config/groups_test.go @@ -40,13 +40,13 @@ func TestLoadGroups(t *testing.T) { hosts, map[string][]string{ "group": { - "detected_linux", "detected_freebsd", + "detected_linux", "host1.example.org", }, "group:remove": { - "host2", "detected_mips", + "host2", }, "group2": { "all", @@ -64,8 +64,8 @@ func TestLoadGroups(t *testing.T) { "all", }, "all_except_some:remove": { - "host1.example.org", "group2", + "host1.example.org", }, "remove": { "host1.example.org", diff --git a/cmd/safcm/config/templates.go b/cmd/safcm/config/templates.go index 2b7508c..e53e167 100644 --- a/cmd/safcm/config/templates.go +++ b/cmd/safcm/config/templates.go @@ -10,7 +10,6 @@ import ( "fmt" "os" "path/filepath" - "slices" "strings" "text/template" @@ -110,9 +109,7 @@ func (t *templateArgs) InGroup(group string) bool { func (t *templateArgs) GroupHosts(group string) []string { t.groupMustExist(group) - x := t.allGroupHosts[group] - slices.Sort(x) - return x + return t.allGroupHosts[group] } func (t *templateArgs) groupMustExist(group string) {