Skip to content

Commit c8d75ca

Browse files
andrey-koestesp
authored andcommitted
do not mutate defaults in replaceOrAppendEnvValues
Signed-off-by: Andrey Kolomentsev <[email protected]> Signed-off-by: akolomentsev <[email protected]>
1 parent 6c6b7e2 commit c8d75ca

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

oci/spec_opts.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,38 +141,40 @@ func WithEnv(environmentVariables []string) SpecOpts {
141141
// replaced by env key or appended to the list
142142
func replaceOrAppendEnvValues(defaults, overrides []string) []string {
143143
cache := make(map[string]int, len(defaults))
144+
results := make([]string, 0, len(defaults))
144145
for i, e := range defaults {
145146
parts := strings.SplitN(e, "=", 2)
147+
results = append(results, e)
146148
cache[parts[0]] = i
147149
}
148150

149151
for _, value := range overrides {
150152
// Values w/o = means they want this env to be removed/unset.
151153
if !strings.Contains(value, "=") {
152154
if i, exists := cache[value]; exists {
153-
defaults[i] = "" // Used to indicate it should be removed
155+
results[i] = "" // Used to indicate it should be removed
154156
}
155157
continue
156158
}
157159

158160
// Just do a normal set/update
159161
parts := strings.SplitN(value, "=", 2)
160162
if i, exists := cache[parts[0]]; exists {
161-
defaults[i] = value
163+
results[i] = value
162164
} else {
163-
defaults = append(defaults, value)
165+
results = append(results, value)
164166
}
165167
}
166168

167169
// Now remove all entries that we want to "unset"
168-
for i := 0; i < len(defaults); i++ {
169-
if defaults[i] == "" {
170-
defaults = append(defaults[:i], defaults[i+1:]...)
170+
for i := 0; i < len(results); i++ {
171+
if results[i] == "" {
172+
results = append(results[:i], results[i+1:]...)
171173
i--
172174
}
173175
}
174176

175-
return defaults
177+
return results
176178
}
177179

178180
// WithProcessArgs replaces the args on the generated spec

0 commit comments

Comments
 (0)