Skip to content

Commit 8bf2167

Browse files
committed
use string-concatenation instead of sprintf for simple cases
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent ecfad5a commit 8bf2167

File tree

16 files changed

+44
-47
lines changed

16 files changed

+44
-47
lines changed

contrib/cmd/recvtty/recvtty.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func main() {
187187
v = append(v, version)
188188
}
189189
if gitCommit != "" {
190-
v = append(v, fmt.Sprintf("commit: %s", gitCommit))
190+
v = append(v, "commit: "+gitCommit)
191191
}
192192
app.Version = strings.Join(v, "\n")
193193

libcontainer/apparmor/apparmor.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ func IsEnabled() bool {
2323
func setProcAttr(attr, value string) error {
2424
// Under AppArmor you can only change your own attr, so use /proc/self/
2525
// instead of /proc/<tid>/ like libapparmor does
26-
path := fmt.Sprintf("/proc/self/attr/%s", attr)
27-
28-
f, err := os.OpenFile(path, os.O_WRONLY, 0)
26+
f, err := os.OpenFile("/proc/self/attr/"+attr, os.O_WRONLY, 0)
2927
if err != nil {
3028
return err
3129
}
@@ -35,14 +33,13 @@ func setProcAttr(attr, value string) error {
3533
return err
3634
}
3735

38-
_, err = fmt.Fprintf(f, "%s", value)
36+
_, err = f.WriteString(value)
3937
return err
4038
}
4139

4240
// changeOnExec reimplements aa_change_onexec from libapparmor in Go
4341
func changeOnExec(name string) error {
44-
value := "exec " + name
45-
if err := setProcAttr("exec", value); err != nil {
42+
if err := setProcAttr("exec", "exec "+name); err != nil {
4643
return fmt.Errorf("apparmor failed to apply profile: %s", err)
4744
}
4845
return nil

libcontainer/cgroups/ebpf/devicefilter/devicefilter.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
package devicefilter
88

99
import (
10-
"fmt"
1110
"math"
11+
"strconv"
1212

1313
"github.com/cilium/ebpf/asm"
1414
"github.com/opencontainers/runc/libcontainer/configs"
@@ -114,9 +114,11 @@ func (p *program) appendDevice(dev *configs.DeviceRule) error {
114114
// If the access is rwm, skip the check.
115115
hasAccess := bpfAccess != (unix.BPF_DEVCG_ACC_READ | unix.BPF_DEVCG_ACC_WRITE | unix.BPF_DEVCG_ACC_MKNOD)
116116

117-
blockSym := fmt.Sprintf("block-%d", p.blockID)
118-
nextBlockSym := fmt.Sprintf("block-%d", p.blockID+1)
119-
prevBlockLastIdx := len(p.insts) - 1
117+
var (
118+
blockSym = "block-" + strconv.Itoa(p.blockID)
119+
nextBlockSym = "block-" + strconv.Itoa(p.blockID+1)
120+
prevBlockLastIdx = len(p.insts) - 1
121+
)
120122
if hasType {
121123
p.insts = append(p.insts,
122124
// if (R2 != bpfType) goto next
@@ -158,7 +160,7 @@ func (p *program) finalize() (asm.Instructions, error) {
158160
// acceptBlock with asm.Return() is already inserted
159161
return p.insts, nil
160162
}
161-
blockSym := fmt.Sprintf("block-%d", p.blockID)
163+
blockSym := "block-" + strconv.Itoa(p.blockID)
162164
p.insts = append(p.insts,
163165
// R0 <- 0
164166
asm.Mov.Imm32(asm.R0, 0).Sym(blockSym),

libcontainer/cgroups/systemd/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func newProp(name string, units interface{}) systemdDbus.Property {
307307
func getUnitName(c *configs.Cgroup) string {
308308
// by default, we create a scope unless the user explicitly asks for a slice.
309309
if !strings.HasSuffix(c.Name, ".slice") {
310-
return fmt.Sprintf("%s-%s.scope", c.ScopePrefix, c.Name)
310+
return c.ScopePrefix + "-" + c.Name + ".scope"
311311
}
312312
return c.Name
313313
}

libcontainer/configs/namespaces_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func IsNamespaceSupported(ns NamespaceType) bool {
5656
if nsFile == "" {
5757
return false
5858
}
59-
_, err := os.Stat(fmt.Sprintf("/proc/self/ns/%s", nsFile))
59+
_, err := os.Stat("/proc/self/ns/" + nsFile)
6060
// a namespace is supported if it exists and we have permissions to read it
6161
supported = err == nil
6262
supportedNamespaces[ns] = supported

libcontainer/container_linux.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os/exec"
1515
"path/filepath"
1616
"reflect"
17+
"strconv"
1718
"strings"
1819
"sync"
1920
"time"
@@ -453,7 +454,7 @@ func (c *linuxContainer) includeExecFifo(cmd *exec.Cmd) error {
453454

454455
cmd.ExtraFiles = append(cmd.ExtraFiles, os.NewFile(uintptr(fifoFd), fifoName))
455456
cmd.Env = append(cmd.Env,
456-
fmt.Sprintf("_LIBCONTAINER_FIFOFD=%d", stdioFdCount+len(cmd.ExtraFiles)-1))
457+
"_LIBCONTAINER_FIFOFD="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1))
457458
return nil
458459
}
459460

@@ -496,24 +497,24 @@ func (c *linuxContainer) commandTemplate(p *Process, childInitPipe *os.File, chi
496497
if cmd.SysProcAttr == nil {
497498
cmd.SysProcAttr = &unix.SysProcAttr{}
498499
}
499-
cmd.Env = append(cmd.Env, fmt.Sprintf("GOMAXPROCS=%s", os.Getenv("GOMAXPROCS")))
500+
cmd.Env = append(cmd.Env, "GOMAXPROCS="+os.Getenv("GOMAXPROCS"))
500501
cmd.ExtraFiles = append(cmd.ExtraFiles, p.ExtraFiles...)
501502
if p.ConsoleSocket != nil {
502503
cmd.ExtraFiles = append(cmd.ExtraFiles, p.ConsoleSocket)
503504
cmd.Env = append(cmd.Env,
504-
fmt.Sprintf("_LIBCONTAINER_CONSOLE=%d", stdioFdCount+len(cmd.ExtraFiles)-1),
505+
"_LIBCONTAINER_CONSOLE="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1),
505506
)
506507
}
507508
cmd.ExtraFiles = append(cmd.ExtraFiles, childInitPipe)
508509
cmd.Env = append(cmd.Env,
509-
fmt.Sprintf("_LIBCONTAINER_INITPIPE=%d", stdioFdCount+len(cmd.ExtraFiles)-1),
510-
fmt.Sprintf("_LIBCONTAINER_STATEDIR=%s", c.root),
510+
"_LIBCONTAINER_INITPIPE="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1),
511+
"_LIBCONTAINER_STATEDIR="+c.root,
511512
)
512513

513514
cmd.ExtraFiles = append(cmd.ExtraFiles, childLogPipe)
514515
cmd.Env = append(cmd.Env,
515-
fmt.Sprintf("_LIBCONTAINER_LOGPIPE=%d", stdioFdCount+len(cmd.ExtraFiles)-1),
516-
fmt.Sprintf("_LIBCONTAINER_LOGLEVEL=%s", p.LogLevel),
516+
"_LIBCONTAINER_LOGPIPE="+strconv.Itoa(stdioFdCount+len(cmd.ExtraFiles)-1),
517+
"_LIBCONTAINER_LOGLEVEL="+p.LogLevel,
517518
)
518519

519520
// NOTE: when running a container with no PID namespace and the parent process spawning the container is
@@ -2033,7 +2034,7 @@ func (c *linuxContainer) bootstrapData(cloneFlags uintptr, nsMaps map[configs.Na
20332034
// write oom_score_adj
20342035
r.AddData(&Bytemsg{
20352036
Type: OomScoreAdjAttr,
2036-
Value: []byte(fmt.Sprintf("%d", *c.config.OomScoreAdj)),
2037+
Value: []byte(strconv.Itoa(*c.config.OomScoreAdj)),
20372038
})
20382039
}
20392040

libcontainer/integration/exec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ func TestHook(t *testing.T) {
12441244

12451245
config := newTemplateConfig(rootfs)
12461246
expectedBundle := bundle
1247-
config.Labels = append(config.Labels, fmt.Sprintf("bundle=%s", expectedBundle))
1247+
config.Labels = append(config.Labels, "bundle="+expectedBundle)
12481248

12491249
getRootfsFromBundle := func(bundle string) (string, error) {
12501250
f, err := os.Open(filepath.Join(bundle, "config.json"))

libcontainer/setns_init_linux.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package libcontainer
44

55
import (
6-
"fmt"
76
"os"
87
"runtime"
98

@@ -25,7 +24,7 @@ type linuxSetnsInit struct {
2524
}
2625

2726
func (l *linuxSetnsInit) getSessionRingName() string {
28-
return fmt.Sprintf("_ses.%s", l.config.ContainerId)
27+
return "_ses." + l.config.ContainerId
2928
}
3029

3130
func (l *linuxSetnsInit) Init() error {

libcontainer/specconv/spec_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) {
224224
}
225225
labels := []string{}
226226
for k, v := range spec.Annotations {
227-
labels = append(labels, fmt.Sprintf("%s=%s", k, v))
227+
labels = append(labels, k+"="+v)
228228
}
229229
config := &configs.Config{
230230
Rootfs: rootfsPath,
231231
NoPivotRoot: opts.NoPivotRoot,
232232
Readonlyfs: spec.Root.Readonly,
233233
Hostname: spec.Hostname,
234-
Labels: append(labels, fmt.Sprintf("bundle=%s", cwd)),
234+
Labels: append(labels, "bundle="+cwd),
235235
NoNewKeyring: opts.NoNewKeyring,
236236
RootlessEUID: opts.RootlessEUID,
237237
RootlessCgroups: opts.RootlessCgroups,

libcontainer/standard_init_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
package libcontainer
44

55
import (
6-
"fmt"
76
"os"
87
"os/exec"
98
"runtime"
9+
"strconv"
1010

1111
"github.com/opencontainers/runc/libcontainer/apparmor"
1212
"github.com/opencontainers/runc/libcontainer/configs"
@@ -40,7 +40,7 @@ func (l *linuxStandardInit) getSessionRingParams() (string, uint32, uint32) {
4040

4141
// Create a unique per session container name that we can join in setns;
4242
// However, other containers can also join it.
43-
return fmt.Sprintf("_ses.%s", l.config.ContainerId), 0xffffffff, newperms
43+
return "_ses." + l.config.ContainerId, 0xffffffff, newperms
4444
}
4545

4646
func (l *linuxStandardInit) Init() error {
@@ -185,7 +185,7 @@ func (l *linuxStandardInit) Init() error {
185185
// user process. We open it through /proc/self/fd/$fd, because the fd that
186186
// was given to us was an O_PATH fd to the fifo itself. Linux allows us to
187187
// re-open an O_PATH fd through /proc.
188-
fd, err := unix.Open(fmt.Sprintf("/proc/self/fd/%d", l.fifoFd), unix.O_WRONLY|unix.O_CLOEXEC, 0)
188+
fd, err := unix.Open("/proc/self/fd/"+strconv.Itoa(l.fifoFd), unix.O_WRONLY|unix.O_CLOEXEC, 0)
189189
if err != nil {
190190
return newSystemErrorWithCause(err, "open exec fifo")
191191
}

0 commit comments

Comments
 (0)