Skip to content

Commit

Permalink
Merge pull request #14 from goodhosts/fix-combineip
Browse files Browse the repository at this point in the history
fix panic in combineIp
  • Loading branch information
luthermonson authored Oct 16, 2020
2 parents 96741dd + 8675487 commit bb6e0f7
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,13 @@ func (h *Hosts) combineIp(ip string) {

linesCopy := make([]HostsLine, len(h.Lines))
copy(linesCopy, h.Lines)
for i, line := range linesCopy {
for _, line := range linesCopy {
if line.IP == ip {
newLine.Combine(line)
h.removeByPosition(i)
}
}
newLine.SortHosts()
h.removeIp(ip)
h.Lines = append(h.Lines, newLine)
}

Expand All @@ -294,9 +294,24 @@ func (h *Hosts) removeByPosition(pos int) {
h.Lines = []HostsLine{}
return
}
if pos == len(h.Lines) {
h.Lines = h.Lines[:pos-1]
return
}
h.Lines = append(h.Lines[:pos], h.Lines[pos+1:]...)
}

func (h *Hosts) removeIp(ip string) {
var newLines []HostsLine
for _, line := range h.Lines {
if line.IP != ip {
newLines = append(newLines, line)
}
}

h.Lines = newLines
}

func (h Hosts) getHostPosition(ip string, host string) int {
for i := range h.Lines {
line := h.Lines[i]
Expand Down

0 comments on commit bb6e0f7

Please sign in to comment.