Don't modify source slice in RemoveFromSlice (#61531)

This commit is contained in:
Przemko Robakowski
2025-11-19 18:33:25 +00:00
committed by GitHub
parent 6d8fca2157
commit 084bc92450
+6 -3
View File
@@ -42,7 +42,6 @@ import (
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/constants"
apiutils "github.com/gravitational/teleport/api/utils"
"github.com/gravitational/teleport/lib/utils/set"
)
// WriteContextCloser provides close method with context
@@ -402,8 +401,12 @@ func GetFreeTCPPorts(n int, offset ...int) (PortList, error) {
// RemoveFromSlice makes a copy of the slice and removes the passed in values from the copy.
func RemoveFromSlice(slice []string, values ...string) []string {
remove := set.New(values...)
return slices.DeleteFunc(slice, func(s string) bool { return remove.Contains(s) })
return slices.DeleteFunc(
slices.Clone(slice),
func(s string) bool {
return slices.Contains(values, s)
},
)
}
// ChooseRandomString returns a random string from the given slice.