Skip to content

Commit b1c42d6

Browse files
authored
Remove non-ipv4 entry from /etc/hosts (#20341)
* Remove none ipv4 entry from /etc/hosts * Fix
1 parent a495f52 commit b1c42d6

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

components/workspacekit/cmd/rings.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -695,22 +695,33 @@ func makeHostnameLocal(ring2root string) error {
695695
if err != nil {
696696
return err
697697
}
698-
b, err := ioutil.ReadFile(path)
698+
b, err := os.ReadFile(path)
699699
if err != nil {
700700
return err
701701
}
702702
bStr := string(b)
703703
lines := strings.Split(bStr, "\n")
704-
for i, line := range lines {
704+
newLines := []string{}
705+
for _, line := range lines {
705706
fields := strings.Fields(line)
706-
if len(fields) != 2 {
707+
if len(fields) < 1 {
708+
newLines = append(newLines, line)
709+
continue
710+
}
711+
if strings.HasPrefix(fields[0], "#") {
712+
newLines = append(newLines, line)
713+
}
714+
ip := net.ParseIP(fields[0]).To4()
715+
if len(ip) != net.IPv4len {
707716
continue
708717
}
709718
if fields[1] == hostname {
710-
lines[i] = "127.0.0.1 " + hostname
719+
newLines = append(newLines, "127.0.0.1 "+hostname)
720+
} else {
721+
newLines = append(newLines, line)
711722
}
712723
}
713-
return ioutil.WriteFile(path, []byte(strings.Join(lines, "\n")), stat.Mode())
724+
return os.WriteFile(path, []byte(strings.Join(newLines, "\n")), stat.Mode())
714725
}
715726

716727
func receiveSeccmpFd(conn *net.UnixConn) (libseccomp.ScmpFd, error) {

0 commit comments

Comments
 (0)