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: Document of AddrFromSlice doesn't mention that passing IPv4 net.IP made with net.ParseIP to AddrFromSlice creates IPv4 mapped IPv6 netip.Addr #54826

Open
YutaroHayakawa opened this issue Sep 2, 2022 · 7 comments
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@YutaroHayakawa
Copy link

YutaroHayakawa commented Sep 2, 2022

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

$ go version
go version go1.19 linux/amd64

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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/yutaro/.cache/go-build"
GOENV="/home/yutaro/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/yutaro/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/yutaro/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2759925210=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://go.dev/play/p/Dnz95xrGio-

What did you expect to see?

isIPv4: true, string: 10.0.0.1
isIPv4: true, string: 10.0.0.1

What did you see instead?

isIPv4: true, string: 10.0.0.1
isIPv4: false, string: ::ffff:10.0.0.1

The issue happens because net.IP holds an IPv4 address in the form of an IPv4 mapped IPv6 address if I understand correctly. Since AddrFromSlice is not aware of the net.IP and it only sees the length of the slice, so it is working as intended in that sense. However, the document of AddrFromSlice is a bit misleading.

Note that a net.IP can be passed directly as the []byte argument.

It can be read like we can use AddrFromSlice to convert net.IP to netip.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 of net.IP before calling AddrFromSlice and need to do net.IP.To4() when it is an IPv4 address. I think users should know this.

@heschi heschi added the NeedsFix The path to resolution is known, but the work has not been done. label Sep 2, 2022
@heschi heschi added this to the Backlog milestone Sep 2, 2022
@heschi
Copy link
Contributor

heschi commented Sep 2, 2022

cc @ianlancetaylor @neild

@ianlancetaylor
Copy link
Contributor

CC @bradfitz @josharian

@SrijanSriv
Copy link

I believe this has been cleared?

@ianwoolf
Copy link
Contributor

i send a cl to add ipv4 check to netip.AddrFromSlice.

https://go-review.googlesource.com/c/go/+/443196

please feel free to review it

@gopherbot
Copy link

Change https://go.dev/cl/443196 mentions this issue: net/netip: update ipv4 check of AddrFromSlice

@ianwoolf
Copy link
Contributor

ianwoolf commented Oct 17, 2022

As the comment of net.IP said, 16-byte slices can still be IPv4 addresses. And IP.String() checks the version of ip and returns a different address string.

But AddrFromSlice only checks the length of the slice. So I think AddrFromSlice should check the ip version when the slice length is 16-byte.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants