-
Notifications
You must be signed in to change notification settings - Fork 18k
net/netip: Document of AddrFromSlice
doesn't mention that passing IPv4 net.IP
made with net.ParseIP
to AddrFromSlice
creates IPv4 mapped IPv6 netip.Addr
#54826
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
Comments
I believe this has been cleared? |
i send a cl to add ipv4 check to https://go-review.googlesource.com/c/go/+/443196 please feel free to review it |
Change https://go.dev/cl/443196 mentions this issue: |
As the comment of But |
In fact, AddrFromSlice over ParseIP or directly produces slightly different results. This is confusing. If you really don't want to change it, at least please document it clearly. func TestAddrFromSlice(t *testing.T) {
addr1, _ := netip.AddrFromSlice(net.ParseIP("192.168.1.1"))
if !addr1.Is4() { // this fails
t.Logf("addr1 is not ipv4: %#v", addr1) // addr1 is not ipv4: netip.Addr{addr:netip.uint128{hi:0x0, lo:0xffffc0a80101}, z:(*intern.Value)(0x140000be018)}
t.Fail()
}
addr2, _ := netip.AddrFromSlice([]byte{192, 168, 1, 1})
if !addr2.Is4() {
t.Logf("addr2 is not ipv4: %#v", addr2)
t.Fail()
}
if !reflect.DeepEqual(addr1, addr2) {
t.Logf("DeepEqual failed, addr1 %#v, addr2 %#v", addr1, addr2)
t.Fail()
}
} |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://go.dev/play/p/Dnz95xrGio-
What did you expect to see?
What did you see instead?
The issue happens because
net.IP
holds an IPv4 address in the form of an IPv4 mapped IPv6 address if I understand correctly. SinceAddrFromSlice
is not aware of thenet.IP
and it only sees the length of the slice, so it is working as intended in that sense. However, the document ofAddrFromSlice
is a bit misleading.It can be read like we can use
AddrFromSlice
to convertnet.IP
tonetip.Addr
, but as mentioned in the #37921,net.IP
cannot distinguish IPv4 address and IPv4 mapped IPv6 address. Thus, users should always be aware of the address family ofnet.IP
before callingAddrFromSlice
and need to donet.IP.To4()
when it is an IPv4 address. I think users should know this.The text was updated successfully, but these errors were encountered: