[GH-ISSUE #25] False assumption: IPs appear in a single position #14

Closed
opened 2026-03-02 05:09:09 +03:00 by kerem · 3 comments
Owner

Originally created by @tomjn on GitHub (Jan 13, 2021).
Original GitHub issue: https://github.com/goodhosts/cli/issues/25

https://github.com/goodhosts/hostsfile/blob/master/hosts.go#L103

The code that adds a host performs a check to see if a line with the IP is already present, and if so, it inspects the hosts on that line and only adds the host if it is not listed.

However, this assumption is not true.

For example, consider this hosts file:

127.0.0.1 tom.test
127.0.0.1 tom.test example.test

Goodhosts will pick up the first line, see that example.test is not present, and add the host, leading to duplication.

Instead, this needs to be a loop, and position := h.getIpPosition(ip) needs to be positions := h.getIpPositions(ip).

Likewise, if an existing entry is present, this code does not remove it, which can lead to hostnames being defined multiple times but with different IPs

Originally created by @tomjn on GitHub (Jan 13, 2021). Original GitHub issue: https://github.com/goodhosts/cli/issues/25 https://github.com/goodhosts/hostsfile/blob/master/hosts.go#L103 The code that adds a host performs a check to see if a line with the IP is already present, and if so, it inspects the hosts on that line and only adds the host if it is not listed. **However, this assumption is not true.** For example, consider this hosts file: ``` 127.0.0.1 tom.test 127.0.0.1 tom.test example.test ``` Goodhosts will pick up the first line, see that `example.test` is not present, and add the host, leading to duplication. Instead, this needs to be a loop, and `position := h.getIpPosition(ip)` needs to be `positions := h.getIpPositions(ip)`. Likewise, if an existing entry is present, this code does not remove it, which can lead to hostnames being defined multiple times but with different IPs
kerem closed this issue 2026-03-02 05:09:10 +03:00
Author
Owner

@tomjn commented on GitHub (Jan 13, 2021):

Likewise getHostPosition getHostPosition and getHostnamePosition need plural versions

<!-- gh-comment-id:759505903 --> @tomjn commented on GitHub (Jan 13, 2021): Likewise `getHostPosition` `getHostPosition` and `getHostnamePosition` need plural versions
Author
Owner

@luthermonson commented on GitHub (Jan 14, 2021):

good find!

<!-- gh-comment-id:760551298 --> @luthermonson commented on GitHub (Jan 14, 2021): good find!
Author
Owner

@tomjn commented on GitHub (Jan 15, 2021):

I notice that in places in the code it uses this and then loops until no instances are found, and in other places it might not do this.

Instead, if it fetched all positions, sorted them from smallest to largest, then worked backwards from the end of the file to the start, we can achieve the same result using these functions. I did start on adding the functions which seemed easy enough, but didn't look into using them, golang isn't super familiar to me. Something like this is what I was thinking:

func (h Hosts) getIpPositions(ip string) []int {
	positions := new [...]int
	for i := range h.Lines {
		line := h.Lines[i]
		if !line.IsComment() && line.Raw != "" && line.IP == ip {
			append(positions, i)
		}
	}

	return positions
}
<!-- gh-comment-id:760808326 --> @tomjn commented on GitHub (Jan 15, 2021): I notice that in places in the code it uses this and then loops until no instances are found, and in other places it might not do this. Instead, if it fetched all positions, sorted them from smallest to largest, then worked backwards from the end of the file to the start, we can achieve the same result using these functions. I did start on adding the functions which seemed easy enough, but didn't look into using them, golang isn't super familiar to me. Something like this is what I was thinking: ```go func (h Hosts) getIpPositions(ip string) []int { positions := new [...]int for i := range h.Lines { line := h.Lines[i] if !line.IsComment() && line.Raw != "" && line.IP == ip { append(positions, i) } } return positions } ```
Sign in to join this conversation.
No labels
bug
bug
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/cli#14
No description provided.