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

proxy: remove redundant length check on local address sets #89792

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/proxy/iptables/proxier.go
Expand Up @@ -1033,7 +1033,7 @@ func (proxier *Proxier) syncProxyRules() {
// If the "external" IP happens to be an IP that is local to this
// machine, hold the local port open so no other process can open it
// (because the socket might open but it would never work).
if localAddrSet.Len() > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && localAddrSet.Has(net.ParseIP(externalIP)) {
if (svcInfo.Protocol() != v1.ProtocolSCTP) && localAddrSet.Has(net.ParseIP(externalIP)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if externalIP is invalid net.ParseIP(externalIP) will be nil and this will panic if there is no localAddrSet.Len() > 0

I've tested it with

func TestIPNets(t *testing.T) {
	s := IPNetSet{}
	if s.Has(nil) {
		fmt.Println("ok")
	}

and panics, however with

	s := IPNetSet{}
	if len(s) > 0 && s.Has(nil) {
		fmt.Println("ok")
	}

it does not panic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's only because you are using an empty set and the Has() method is never called. If there was an invalid externalIP and we still had len > 0 local addresses it would still panic.

lp := utilproxy.LocalPort{
Description: "externalIP for " + svcNameString,
IP: externalIP,
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/ipvs/proxier.go
Expand Up @@ -1216,7 +1216,7 @@ func (proxier *Proxier) syncProxyRules() {
// If the "external" IP happens to be an IP that is local to this
// machine, hold the local port open so no other process can open it
// (because the socket might open but it would never work).
if localAddrSet.Len() > 0 && (svcInfo.Protocol() != v1.ProtocolSCTP) && localAddrSet.Has(net.ParseIP(externalIP)) {
if (svcInfo.Protocol() != v1.ProtocolSCTP) && localAddrSet.Has(net.ParseIP(externalIP)) {
// We do not start listening on SCTP ports, according to our agreement in the SCTP support KEP
lp := utilproxy.LocalPort{
Description: "externalIP for " + svcNameString,
Expand Down