]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
config: fix data race in template's GroupHosts
authorSimon Ruderich <simon.ruderich@fau.de>
Wed, 4 Feb 2026 17:29:29 +0000 (18:29 +0100)
committerSimon Ruderich <simon.ruderich@fau.de>
Thu, 5 Feb 2026 16:01:09 +0000 (17:01 +0100)
==================
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
  [...]

cmd/safcm/config/groups.go
cmd/safcm/config/groups_test.go
cmd/safcm/config/templates.go

index 626c053b7669a328303cde8300251dfdb317ef3d..4a9b3659011450cd7a1bae94c1ac6546478f3749 100644 (file)
@@ -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
index bba47d94b6be815103a89dca8d4142f5653c71df..d0cb9b4f0f04fa7ac0e7ae03e0f178d035ca0d24 100644 (file)
@@ -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",
index 2b7508c1309f2c7063f45d4f603b0b02ed3fe5a2..e53e1674aff12f07ff3224c80c3a9b70de4ced63 100644 (file)
@@ -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) {