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 panics with nil receiver #50271

Closed
Beginner18 opened this issue Dec 20, 2021 · 2 comments
Closed

net: IPNet.String panics with nil receiver #50271

Beginner18 opened this issue Dec 20, 2021 · 2 comments
Labels
FrozenDueToAge help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Beginner18
Copy link

Beginner18 commented Dec 20, 2021

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

1.15.5

Does this issue reproduce with the latest release?

YES

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

go env Output
$ go env

What did you do?

panic, if IPNet is nil

func (n *IPNet) String() string {
	nn, m := networkNumberAndMask(n)
	if nn == nil || m == nil {
		return "<nil>"
	}
	l := simpleMaskLength(m)
	if l == -1 {
		return nn.String() + "/" + m.String()
	}
	return nn.String() + "/" + uitoa(uint(l))
}

What did you expect to see?

IPNet.String() should not panic, even IPNet is nil

What did you see instead?

    panic(0x194abc0, 0x2156090)
        	/usr/local/go/src/runtime/panic.go:969 +0x1b9
        net.networkNumberAndMask(0x0, 0x0, 0x10, 0x23bb108, 0xc000123d30, 0xc00003ed00, 0xc000077dd0)
        	/usr/local/go/src/net/ip.go:476 +0x26
        net.(*IPNet).String(0x0, 0xc00011f470, 0x18eed80)
        	/usr/local/go/src/net/ip.go:526 +0x45
@Beginner18 Beginner18 changed the title affected/package: net/ip.go IPNet.String panic Dec 20, 2021
@ianlancetaylor ianlancetaylor changed the title net/ip.go IPNet.String panic net: IPNet.String panic Dec 20, 2021
@ianlancetaylor ianlancetaylor changed the title net: IPNet.String panic net: IPNet.String panics with nil receiver Dec 20, 2021
@ianlancetaylor ianlancetaylor added help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Dec 20, 2021
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Dec 20, 2021
@ianlancetaylor
Copy link
Contributor

While in general the standard library is happy to panic when invoked on an invalid value, I agree that perhaps the String method should not do so.

@gopherbot
Copy link

Change https://golang.org/cl/373075 mentions this issue: net: do not panic on nil IPNet.String()

jproberts pushed a commit to jproberts/go that referenced this issue Jun 21, 2022
The code looks like it was already trying to avoid this but missed a
spot.

Fixes golang#50271.

Change-Id: I450adac3f618b9535b61a28e6a160eacc351d47c
Reviewed-on: https://go-review.googlesource.com/c/go/+/373075
Trust: Jason Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@golang golang locked and limited conversation to collaborators Jan 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants