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/netip: Prefix.Bits and Prefix.IsValid are inconsistent #54525

Closed
dsnet opened this issue Aug 18, 2022 · 2 comments
Closed

net/netip: Prefix.Bits and Prefix.IsValid are inconsistent #54525

dsnet opened this issue Aug 18, 2022 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Aug 18, 2022

The documentation for Prefix.Bits says:

Bits returns p's prefix length.
It reports -1 if invalid.

The documentation for Prefix.IsValid says:

IsValid reports whether p.Bits() has a valid range for p.IP().
If p.Addr() is the zero Addr, IsValid returns false.
Note that if p is the zero Prefix, then p.IsValid() == false.

Thus, we expect the zero Prefix to be invalid, but also report -1 bits.
Instead, we get 0 bits.

https://go.dev/play/p/rsc2Uy_jMX1

Either the behavior or the documentation should be adjusted.

\cc @bradfitz @josharian

@gopherbot
Copy link

Change https://go.dev/cl/424816 mentions this issue: net/netip: report -1 from Prefix.Bits() when Prefix is invalid

@joedian joedian added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 19, 2022
@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
@gopherbot
Copy link

Change https://go.dev/cl/425035 mentions this issue: net/netip: fix invalid representation of Prefix

johanbrandhorst pushed a commit to Pryz/go that referenced this issue Feb 12, 2023
For a given Addr, ensure there is exactly one invalid representation.
This allows invalid representations to be safely comparable.
To ensure that the zero value of Prefix is invalid,
we modify the encoding of bits to simply be the bit count plus one.

Since Addr is immutable, we check in the PrefixFrom constructor that
the provided Addr is valid and only store a non-zero bits length if so.
IsValid is simplified to just checking whether bitsPlusOne is non-zero.

Fixes golang#54525

Change-Id: I9244cae2fd160cc9c81d007866992df2e422d3b9
Reviewed-on: https://go-review.googlesource.com/c/go/+/425035
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
@golang golang locked and limited conversation to collaborators Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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

4 participants