Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slice bounds out of range error on remove host entries #46

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions pkg/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,6 @@ func (h *Hosts) Remove(hosts []string) error {
return err
}

uniqueHosts := map[string]bool{}
for i := 0; i < len(hosts); i++ {
uniqueHosts[hosts[i]] = true
}

var hostEntries = make(map[string]struct{}, len(uniqueHosts))

for key := range uniqueHosts {
hostEntries[key] = struct{}{}
}

praveenkumar marked this conversation as resolved.
Show resolved Hide resolved
start, end := h.findCrcSection()

h.Lock()
Expand All @@ -196,11 +185,13 @@ func (h *Hosts) Remove(hosts []string) error {
continue
}

for hostIdx, hostname := range line.Hostnames {
if _, ok := hostEntries[hostname]; ok {
h.removeHostFromLine(line, hostIdx, i)
for _, hostToRemove := range hosts {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

for hostIdx, hostname := range line.Hostnames {
if hostname == hostToRemove {
h.removeHostFromLine(line, hostIdx, i)
break
}
}

}
}
} else {
Expand Down
12 changes: 12 additions & 0 deletions pkg/hosts/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ func TestRemoveOnOldHostFile(t *testing.T) {
assert.Equal(t, hostsTemplate, string(content))
}

func TestDeleteSliceBoundErrorOnRemove(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

dir, err := os.MkdirTemp("", "hosts")
assert.NoError(t, err)
defer os.RemoveAll(dir)

hostsFile := filepath.Join(dir, "hosts")
assert.NoError(t, os.WriteFile(hostsFile, []byte(hostsTemplate+eol()+crcSection("192.168.130.11 api.crc.testing canary-openshift-ingress-canary.apps-crc.testing console-openshift-console.apps-crc.testing default-route-openshift-image-registry.apps-crc.testing downloads-openshift-console.apps-crc.testing oauth-openshift.apps-crc.testing")), 0600))
host := hosts(t, hostsFile)

assert.NoError(t, host.Remove([]string{"api.crc.testing", "oauth-openshift.apps-crc.testing", "console-openshift-console.apps-crc.testing", "downloads-openshift-console.apps-crc.testing", "canary-openshift-ingress-canary.apps-crc.testing", "default-route-openshift-image-registry.apps-crc.testing"}))
}

func hosts(t *testing.T, hostsFile string) Hosts {
config, _ := libhosty.NewHostsFileConfig(hostsFile)
file, err := libhosty.InitWithConfig(config)
Expand Down