Skip to content

Commit 1c15312

Browse files
committed
Use maps.Clone more
There is a slight functional change here that may not be safe: - before this patch, if the source map is nil, the destination map will be empty (but non-nil); - after this patch, the destination map is nil if the source one is. Practically, it may or may not matter, as reading from the nil map is ok, and only changing it is not (will result in panic). Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent ad243f9 commit 1c15312

File tree

6 files changed

+20
-60
lines changed

6 files changed

+20
-60
lines changed

libpod/container.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"maps"
1011
"net"
1112
"os"
1213
"strings"
@@ -624,13 +625,9 @@ func (c *Container) Stdin() bool {
624625
return c.config.Stdin
625626
}
626627

627-
// Labels returns the container's labels
628+
// Labels returns the container's labels.
628629
func (c *Container) Labels() map[string]string {
629-
labels := make(map[string]string)
630-
for key, value := range c.config.Labels {
631-
labels[key] = value
632-
}
633-
return labels
630+
return maps.Clone(c.config.Labels)
634631
}
635632

636633
// StopSignal is the signal that will be used to stop the container
@@ -1038,13 +1035,7 @@ func (c *Container) BindMounts() (map[string]string, error) {
10381035
}
10391036
}
10401037

1041-
newMap := make(map[string]string, len(c.state.BindMounts))
1042-
1043-
for key, val := range c.state.BindMounts {
1044-
newMap[key] = val
1045-
}
1046-
1047-
return newMap, nil
1038+
return maps.Clone(c.state.BindMounts), nil
10481039
}
10491040

10501041
// StoppedByUser returns whether the container was last stopped by an explicit

libpod/options.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -694,17 +694,14 @@ func (r *Runtime) WithPod(pod *Pod) CtrCreateOption {
694694
}
695695
}
696696

697-
// WithLabels adds labels to the container.
697+
// WithLabels sets the container's labels.
698698
func WithLabels(labels map[string]string) CtrCreateOption {
699699
return func(ctr *Container) error {
700700
if ctr.valid {
701701
return define.ErrCtrFinalized
702702
}
703703

704-
ctr.config.Labels = make(map[string]string)
705-
for key, value := range labels {
706-
ctr.config.Labels[key] = value
707-
}
704+
ctr.config.Labels = maps.Clone(labels)
708705

709706
return nil
710707
}
@@ -1619,10 +1616,7 @@ func WithVolumeLabels(labels map[string]string) VolumeCreateOption {
16191616
return define.ErrVolumeFinalized
16201617
}
16211618

1622-
volume.config.Labels = make(map[string]string)
1623-
for key, value := range labels {
1624-
volume.config.Labels[key] = value
1625-
}
1619+
volume.config.Labels = maps.Clone(labels)
16261620

16271621
return nil
16281622
}
@@ -1647,10 +1641,7 @@ func WithVolumeOptions(options map[string]string) VolumeCreateOption {
16471641
return define.ErrVolumeFinalized
16481642
}
16491643

1650-
volume.config.Options = make(map[string]string)
1651-
for key, value := range options {
1652-
volume.config.Options[key] = value
1653-
}
1644+
volume.config.Options = maps.Clone(options)
16541645

16551646
return nil
16561647
}
@@ -2018,10 +2009,7 @@ func WithPodLabels(labels map[string]string) PodCreateOption {
20182009
return define.ErrPodFinalized
20192010
}
20202011

2021-
pod.config.Labels = make(map[string]string)
2022-
for key, value := range labels {
2023-
pod.config.Labels[key] = value
2024-
}
2012+
pod.config.Labels = maps.Clone(labels)
20252013

20262014
return nil
20272015
}

libpod/pod.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package libpod
55
import (
66
"errors"
77
"fmt"
8+
"maps"
89
"sort"
910
"strings"
1011
"time"
@@ -285,12 +286,7 @@ func (p *Pod) VolumesFrom() []string {
285286

286287
// Labels returns the pod's labels
287288
func (p *Pod) Labels() map[string]string {
288-
labels := make(map[string]string)
289-
for key, value := range p.config.Labels {
290-
labels[key] = value
291-
}
292-
293-
return labels
289+
return maps.Clone(p.config.Labels)
294290
}
295291

296292
// CreatedTime gets the time when the pod was created

libpod/volume.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package libpod
55
import (
66
"fmt"
77
"io"
8+
"maps"
89
"time"
910

1011
"github.com/containers/podman/v5/libpod/define"
@@ -136,13 +137,9 @@ func (v *Volume) Scope() string {
136137
return "local"
137138
}
138139

139-
// Labels returns the volume's labels
140+
// Labels returns the volume's labels.
140141
func (v *Volume) Labels() map[string]string {
141-
labels := make(map[string]string)
142-
for key, value := range v.config.Labels {
143-
labels[key] = value
144-
}
145-
return labels
142+
return maps.Clone(v.config.Labels)
146143
}
147144

148145
// MountPoint returns the volume's mountpoint on the host
@@ -180,13 +177,9 @@ func (v *Volume) mountPoint() string {
180177
return v.config.MountPoint
181178
}
182179

183-
// Options return the volume's options
180+
// Options return the volume's options.
184181
func (v *Volume) Options() map[string]string {
185-
options := make(map[string]string)
186-
for k, v := range v.config.Options {
187-
options[k] = v
188-
}
189-
return options
182+
return maps.Clone(v.config.Options)
190183
}
191184

192185
// Anonymous returns whether this volume is anonymous. Anonymous volumes were

libpod/volume_inspect.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package libpod
44

55
import (
66
"fmt"
7+
"maps"
78

89
"github.com/containers/podman/v5/libpod/define"
910
pluginapi "github.com/docker/go-plugins-helpers/volume"
@@ -51,15 +52,9 @@ func (v *Volume) Inspect() (*define.InspectVolumeData, error) {
5152
data.Name = v.config.Name
5253
data.Driver = v.config.Driver
5354
data.CreatedAt = v.config.CreatedTime
54-
data.Labels = make(map[string]string)
55-
for k, v := range v.config.Labels {
56-
data.Labels[k] = v
57-
}
55+
data.Labels = maps.Clone(v.config.Labels)
5856
data.Scope = v.Scope()
59-
data.Options = make(map[string]string)
60-
for k, v := range v.config.Options {
61-
data.Options[k] = v
62-
}
57+
data.Options = maps.Clone(v.config.Options)
6358
data.UID = v.uid()
6459
data.GID = v.gid()
6560
data.Anonymous = v.config.IsAnon

test/testvol/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,8 @@ func (d *DirDriver) Create(opts *volume.CreateRequest) error {
153153
newVol := new(dirVol)
154154
newVol.name = opts.Name
155155
newVol.mounts = make(map[string]bool)
156-
newVol.options = make(map[string]string)
156+
newVol.options = maps.Clone(opts.Options)
157157
newVol.createTime = time.Now()
158-
for k, v := range opts.Options {
159-
newVol.options[k] = v
160-
}
161158

162159
volPath := filepath.Join(d.volumesPath, opts.Name)
163160
if err := os.Mkdir(volPath, 0755); err != nil {

0 commit comments

Comments
 (0)