Skip to content

Commit

Permalink
Replace uses of slices.Clone()
Browse files Browse the repository at this point in the history
Avoid golang/go#64759

Co-authored-by: Bjorn Neergaard <bjorn.neergaard@docker.com>
Signed-off-by: Rob Murray <rob.murray@docker.com>
  • Loading branch information
2 people authored and Dzejrou committed Mar 5, 2024
1 parent 384f654 commit 3ed2ed8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
12 changes: 5 additions & 7 deletions libnetwork/internal/resolvconf/resolvconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
"bufio"
"bytes"
"context"
_ "embed"
"fmt"
"io"
"io/fs"
"net/netip"
"os"
"slices"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -141,7 +139,7 @@ func (rc *ResolvConf) SetHeader(c string) {

// NameServers returns addresses used in nameserver directives.
func (rc *ResolvConf) NameServers() []netip.Addr {
return slices.Clone(rc.nameServers)
return append([]netip.Addr(nil), rc.nameServers...)
}

// OverrideNameServers replaces the current set of nameservers.
Expand All @@ -152,7 +150,7 @@ func (rc *ResolvConf) OverrideNameServers(nameServers []netip.Addr) {

// Search returns the current DNS search domains.
func (rc *ResolvConf) Search() []string {
return slices.Clone(rc.search)
return append([]string(nil), rc.search...)
}

// OverrideSearch replaces the current DNS search domains.
Expand All @@ -169,7 +167,7 @@ func (rc *ResolvConf) OverrideSearch(search []string) {

// Options returns the current options.
func (rc *ResolvConf) Options() []string {
return slices.Clone(rc.options)
return append([]string(nil), rc.options...)
}

// Option finds the last option named search, and returns (value, true) if
Expand All @@ -192,7 +190,7 @@ func (rc *ResolvConf) Option(search string) (string, bool) {

// OverrideOptions replaces the current DNS options.
func (rc *ResolvConf) OverrideOptions(options []string) {
rc.options = slices.Clone(options)
rc.options = append([]string(nil), options...)
rc.md.NDotsFrom = ""
if _, exists := rc.Option("ndots"); exists {
rc.md.NDotsFrom = "override"
Expand Down Expand Up @@ -314,7 +312,7 @@ func (rc *ResolvConf) TransformForIntNS(
}

rc.md.Transform = "internal resolver"
return slices.Clone(rc.md.ExtNameServers), nil
return append([]ExtDNSEntry(nil), rc.md.ExtNameServers...), nil
}

// Generate returns content suitable for writing to a resolv.conf file. If comments
Expand Down
8 changes: 4 additions & 4 deletions libnetwork/internal/resolvconf/resolvconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ func TestRCModify(t *testing.T) {
rc.OverrideSearch(tc.overrideSearch)
rc.OverrideOptions(tc.overrideOptions)

assert.Check(t, is.DeepEqual(rc.NameServers(), overrideNS, cmpopts.EquateComparable(netip.Addr{})))
assert.Check(t, is.DeepEqual(rc.Search(), tc.overrideSearch))
assert.Check(t, is.DeepEqual(rc.Options(), tc.overrideOptions))
assert.Check(t, is.DeepEqual(rc.NameServers(), overrideNS, cmpopts.EquateEmpty(), cmpopts.EquateComparable(netip.Addr{})))
assert.Check(t, is.DeepEqual(rc.Search(), tc.overrideSearch, cmpopts.EquateEmpty()))
assert.Check(t, is.DeepEqual(rc.Options(), tc.overrideOptions, cmpopts.EquateEmpty()))
}

if tc.addOption != "" {
options := rc.Options()
rc.AddOption(tc.addOption)
assert.Check(t, is.DeepEqual(rc.Options(), append(options, tc.addOption)))
assert.Check(t, is.DeepEqual(rc.Options(), append(options, tc.addOption), cmpopts.EquateEmpty()))
}

d := t.TempDir()
Expand Down

0 comments on commit 3ed2ed8

Please sign in to comment.