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

net: IPNet.String() returns an unexpected string value '<nil>' #39516

Closed
colinmcintosh opened this issue Jun 11, 2020 · 2 comments
Closed

net: IPNet.String() returns an unexpected string value '<nil>' #39516

colinmcintosh opened this issue Jun 11, 2020 · 2 comments
Milestone

Comments

@colinmcintosh
Copy link

@colinmcintosh colinmcintosh commented Jun 11, 2020

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

$ go version
go version go1.13.7 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

Not applicable.

What did you do?

When calling the .String() method on net.IPNet the method will return a string containing the text "<nil>" rather than a zero-value string or a nil pointer. This arbitrary string behavior is not documented and was an unexpected response to indicate an inability to string the IPNet data.

Can this be changed to return a proper string zero-value?

package main

import (
	"fmt"
	"net"
)

func main() {
	ip := new(net.IPNet)
	ip.IP = []byte("127.0.0.1")
	// ip.Mask not set
	fmt.Printf("%T: %s\n", ip.String(), ip.String()) // string: <nil>
}

What did you expect to see?

An empty string.

What did you see instead?

An arbitrary string containing the text "<nil>".

@antong
Copy link
Contributor

@antong antong commented Jun 11, 2020

Thank you for the report and for the clear code example!

I don't think an empty string would be any less arbitrary. I might even argue it could be ok for String() to panic for an uninitialized IPNet (nil fields). Also, the string "<nil>" is not that uncommon for things similar to this. fmt for example will output "<nil>" for nil pointers and for String() pointer receivers that panic because of dereferencing a nil pointer (Example: https://play.golang.org/p/f2gNtD7W8Pl). So, I would say returning "<nil>" is an option as good as any, and the function is good as it is.

How did you end up with an unitialized IPNet in the first place? The example code you provided looks like a mistake as it assigns to the IP the bytes of the string representation of 127.0.0.1 ([]byte("127.0.0.1"), when it should be []byte{127,0,0,1} or even clearer, IPv4(127, 0, 0, 1). Perhaps the net.ParseCIDR() function is what you are looking for? Using that, you would not end up with a partially initialized IPNet.

@toothrot toothrot changed the title net.IPNet.String() returns an unexpected string value '<nil>' net: IPNet.String() returns an unexpected string value '<nil>' Jun 12, 2020
@toothrot toothrot added this to the Backlog milestone Jun 12, 2020
@toothrot
Copy link
Contributor

@toothrot toothrot commented Jun 12, 2020

I agree with @antong's helpful response here. I'm closing this, as I don't believe this is an issue with the standard library. Please comment if I am mistaken.

@toothrot toothrot closed this Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.