golang/go

net: `interface_linux.go` has redundant code

Open

#62,683 opened on Sep 17, 2023

View on GitHub
 (6 comments) (0 reactions) (0 assignees)Go (19,008 forks)batch import
NeedsInvestigationOS-Linuxhelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

What version of Go are you using (go version)?

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

What did you do?

func addrTable(ift []Interface, ifi *Interface, msgs []syscall.NetlinkMessage) ([]Addr, error) {
	var ifat []Addr
loop:
	for _, m := range msgs {
		switch m.Header.Type {
		case syscall.NLMSG_DONE:
			break loop
		case syscall.RTM_NEWADDR:
			ifam := (*syscall.IfAddrmsg)(unsafe.Pointer(&m.Data[0]))
			if len(ift) != 0 || ifi.Index == int(ifam.Index) {
				if len(ift) != 0 {
					var err error
					ifi, err = interfaceByIndex(ift, int(ifam.Index))
					if err != nil {
						return nil, err
					}
				}
				attrs, err := syscall.ParseNetlinkRouteAttr(&m)
				if err != nil {
					return nil, os.NewSyscallError("parsenetlinkrouteattr", err)
				}
				ifa := newAddr(ifam, attrs)
				if ifa != nil {
					ifat = append(ifat, ifa)
				}
			}
		}
	}
	return ifat, nil
}

In the above code, the following code logic seems to be an invalid code logic.

if len(ift) != 0 {
    var err error
    ifi, err = interfaceByIndex(ift, int(ifam.Index))
    if err != nil {
        return nil, err
    }
}

len(ift) != 0 || ifi.Index == int(ifam.Index) never executes ifi.Index == int(ifam.Index) when len(ift) != 0, therefore reassigning ifi has no effect.

What did you expect to see?

What did you see instead?

Contributor guide