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

Unnecessary non-Nil Check in ipallocator.go #125512

Closed
darren opened this issue Jun 14, 2024 · 7 comments · Fixed by #125514
Closed

Unnecessary non-Nil Check in ipallocator.go #125512

darren opened this issue Jun 14, 2024 · 7 comments · Fixed by #125514
Assignees
Labels
sig/network Categorizes an issue or PR as relevant to SIG Network. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@darren
Copy link

darren commented Jun 14, 2024

prefix, err := netip.ParsePrefix(cidr.String())
if err != nil {
return nil, err
}
if prefix.Addr().Is6() && prefix.Bits() < 64 {
return nil, fmt.Errorf("shortest allowed prefix length for service CIDR is 64, got %d", prefix.Bits())
}
// TODO: use the utils/net function once is available
size := hostsPerNetwork(cidr)
var family api.IPFamily
if netutils.IsIPv6CIDR(cidr) {
family = api.IPv6Protocol
} else {
family = api.IPv4Protocol
}
// Caching the first, offset and last addresses allows to optimize
// the search loops by using the netip.Addr iterator instead
// of having to do conversions with IP addresses.
// Don't allocate the network's ".0" address.
ipFirst := prefix.Masked().Addr().Next()
if err != nil {
return nil, err
}

The err is already checked on line 84, so it will always be nil On line 107, the check is redundant

@k8s-ci-robot k8s-ci-robot added needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 14, 2024
@mauri870
Copy link
Member

That is indeed redundant. It seems like someone forgot to remove after a refactor but It was added this way in b022475.

@mauri870
Copy link
Member

@darren Feel free to send a pull request by following https://kubernetes.io/docs/contribute/. Thanks.

@darren
Copy link
Author

darren commented Jun 14, 2024

@mauri870

Thanks! I am wondering whether it is necessary to check ip address returned by calling Next() or Prev() is IsValid()

As in https://github.com/golang/go/blob/a85b8810c49515c469d265c399febfa48442a983/src/net/netip/netip.go#L741-L755

But I don't find the overflowed cases yet.

@darren darren changed the title Unnecessary Nil Check in ipallocator.go Unnecessary non-Nil Check in ipallocator.go Jun 14, 2024
@DrAuYueng
Copy link
Contributor

/sig network

@k8s-ci-robot k8s-ci-robot added sig/network Categorizes an issue or PR as relevant to SIG Network. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jun 14, 2024
@mauri870
Copy link
Member

/assign DrAuYueng

@mauri870
Copy link
Member

mauri870 commented Jun 14, 2024

@mauri870

Thanks! I am wondering whether it is necessary to check ip address returned by calling Next() or Prev() is IsValid()

As in https://github.com/golang/go/blob/a85b8810c49515c469d265c399febfa48442a983/src/net/netip/netip.go#L741-L755

But I don't find the overflowed cases yet.

Hmmm, in theory yes, since .Next() and .Prev() can return an empty netip.Addr. In practice, they probably never overflow in this particular case.

Adding a check for .IsValid() for both Prev and Next would not hurt. I don't expect the path of !ip.IsValid() to be ever taken in these cases, but if they do then that is a bug and it should propagate an error.

@mauri870
Copy link
Member

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/network Categorizes an issue or PR as relevant to SIG Network. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants