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: unexpected padding byte when convert from net.IP to []byte #28571

Closed
leslie-qiwa opened this issue Nov 2, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@leslie-qiwa
Copy link

commented Nov 2, 2018

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

$ go version
go version go1.11 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
GOARCH="amd64"
GOBIN=""
GOCACHE="/go/src/github.com/pensando/test-infra/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build092800278=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/gtcQZajAmBN

package main

import (
"fmt"
"net"
)

func main() {
myIP, _, _ := net.ParseCIDR("127.0.0.1/24")
fmt.Printf("%v\n", []byte(myIP))
}

What did you expect to see?

[127 0 0 1]

What did you see instead?

[0 0 0 0 0 0 0 0 0 0 255 255 127 0 0 1]

@dominikh

This comment has been minimized.

Copy link
Member

commented Nov 2, 2018

From the documentation:

Note that in this documentation, referring to an IP address as an IPv4 address or an IPv6 address is a semantic property of the address, not just the length of the byte slice: a 16-byte slice can still be an IPv4 address.

In particular, you're seeing an IPv4 address embedded in an IPv6 address.

If you want a 4-byte representation, use the To4 method.

This is working as intended.

@dominikh dominikh closed this Nov 2, 2018

@dominikh dominikh changed the title unexpected padding byte when convert from net.IP to []byte net: unexpected padding byte when convert from net.IP to []byte Nov 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.