From 17570625c02672cc9a0b9b4f007003ef5847452c Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 28 Mar 2025 18:52:42 -0700 Subject: [PATCH] Use for range over integers This appears in Go 1.22 (see https://tip.golang.org/ref/spec#For_range). Signed-off-by: Kir Kolyshkin --- delete.go | 2 +- libcontainer/criu_linux.go | 4 ++-- libcontainer/factory_linux.go | 2 +- libcontainer/integration/bench_test.go | 2 +- libcontainer/integration/execin_test.go | 4 ++-- libcontainer/integration/update_test.go | 2 +- libcontainer/process_linux.go | 2 +- libcontainer/rootfs_linux.go | 4 ++-- libcontainer/specconv/spec_linux.go | 2 +- 9 files changed, 12 insertions(+), 12 deletions(-) diff --git a/delete.go b/delete.go index fc8133438..dcb19be1b 100644 --- a/delete.go +++ b/delete.go @@ -15,7 +15,7 @@ import ( func killContainer(container *libcontainer.Container) error { _ = container.Signal(unix.SIGKILL) - for i := 0; i < 100; i++ { + for range 100 { time.Sleep(100 * time.Millisecond) if err := container.Signal(unix.Signal(0)); err != nil { return container.Destroy() diff --git a/libcontainer/criu_linux.go b/libcontainer/criu_linux.go index 30227289e..0a050bf79 100644 --- a/libcontainer/criu_linux.go +++ b/libcontainer/criu_linux.go @@ -832,7 +832,7 @@ func logCriuErrors(dir, file string) { logrus.Warn("...") } // Print the last lines. - for add := 0; add < max; add++ { + for add := range max { i := (idx + add) % max s := lines[i] actLineNo := lineNo + add - max + 1 @@ -961,7 +961,7 @@ func (c *Container) criuSwrk(process *Process, req *criurpc.CriuReq, opts *CriuO val := reflect.ValueOf(req.GetOpts()) v := reflect.Indirect(val) - for i := 0; i < v.NumField(); i++ { + for i := range v.NumField() { st := v.Type() name := st.Field(i).Name if 'A' <= name[0] && name[0] <= 'Z' { diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index 539726dfd..d222d6b42 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -191,7 +191,7 @@ func validateID(id string) error { } // Allowed characters: 0-9 A-Z a-z _ + - . - for i := 0; i < len(id); i++ { + for i := range len(id) { c := id[i] switch { case c >= 'a' && c <= 'z': diff --git a/libcontainer/integration/bench_test.go b/libcontainer/integration/bench_test.go index 841d3aa06..c10c57fda 100644 --- a/libcontainer/integration/bench_test.go +++ b/libcontainer/integration/bench_test.go @@ -60,7 +60,7 @@ func genBigEnv(count int) []string { } envs := make([]string, count) - for i := 0; i < count; i++ { + for i := range count { key := strings.ToUpper(randStr(10)) value := randStr(20) envs[i] = key + "=" + value diff --git a/libcontainer/integration/execin_test.go b/libcontainer/integration/execin_test.go index 831a43744..be4142dc9 100644 --- a/libcontainer/integration/execin_test.go +++ b/libcontainer/integration/execin_test.go @@ -209,7 +209,7 @@ func TestExecInError(t *testing.T) { }() ok(t, err) - for i := 0; i < 42; i++ { + for range 42 { unexistent := &libcontainer.Process{ Cwd: "/", Args: []string{"unexistent"}, @@ -263,7 +263,7 @@ func TestExecInTTY(t *testing.T) { // Repeat to increase chances to catch a race; see // https://github.com/opencontainers/runc/issues/2425. - for i := 0; i < 300; i++ { + for range 300 { var stdout bytes.Buffer parent, child, err := utils.NewSockPair("console") diff --git a/libcontainer/integration/update_test.go b/libcontainer/integration/update_test.go index eb864f7fe..3c0fa4b52 100644 --- a/libcontainer/integration/update_test.go +++ b/libcontainer/integration/update_test.go @@ -60,7 +60,7 @@ func testUpdateDevices(t *testing.T, systemd bool) { } defaultDevices := config.Cgroups.Resources.Devices - for i := 0; i < 300; i++ { + for i := range 300 { // Check the access buf.Reset() err = container.Run(devCheck) diff --git a/libcontainer/process_linux.go b/libcontainer/process_linux.go index 5ac574bde..3fcc79df4 100644 --- a/libcontainer/process_linux.go +++ b/libcontainer/process_linux.go @@ -877,7 +877,7 @@ func getPipeFds(pid int) ([]string, error) { fds := make([]string, 3) dirPath := filepath.Join("/proc", strconv.Itoa(pid), "/fd") - for i := 0; i < 3; i++ { + for i := range 3 { // XXX: This breaks if the path is not a valid symlink (which can // happen in certain particularly unlucky mount namespace setups). f := filepath.Join(dirPath, strconv.Itoa(i)) diff --git a/libcontainer/rootfs_linux.go b/libcontainer/rootfs_linux.go index e1e2d7634..7d7e957dd 100644 --- a/libcontainer/rootfs_linux.go +++ b/libcontainer/rootfs_linux.go @@ -878,7 +878,7 @@ func reOpenDevNull() error { if err := unix.Fstat(int(file.Fd()), &devNullStat); err != nil { return &os.PathError{Op: "fstat", Path: file.Name(), Err: err} } - for fd := 0; fd < 3; fd++ { + for fd := range 3 { if err := unix.Fstat(fd, &stat); err != nil { return &os.PathError{Op: "fstat", Path: "fd " + strconv.Itoa(fd), Err: err} } @@ -1211,7 +1211,7 @@ func remountReadonly(m *configs.Mount) error { dest = m.Destination flags = m.Flags ) - for i := 0; i < 5; i++ { + for range 5 { // There is a special case in the kernel for // MS_REMOUNT | MS_BIND, which allows us to change only the // flags even as an unprivileged user (i.e. user namespace) diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index 1b6f71cd7..c6bf02c98 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -626,7 +626,7 @@ func checkPropertyName(s string) error { } // Check ASCII characters rather than Unicode runes, // so we have to use indexes rather than range. - for i := 0; i < len(s); i++ { + for i := range len(s) { ch := s[i] if (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') { continue