You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\xxxxxx\AppData\Local\go-build
set GOENV=C:\Users\xxxxxx\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\xxxxxx\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\xxxxxx\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\xxxxxx\AppData\Local\Temp\go-build2918900668=/tmp/go-build -gno-record-gcc-switches
What did you do?
I run a C code with CGO.
Use C.CBytes to Convert Go Bytes to C Bytes.
The C function puts takes a pointer to a sequence of char values that is terminated by a NUL byte. The Go expression []byte("go") returns a slice containing two bytes, with no NUL byte. The C.CBytes function takes a byte slice, copies it into C memory as a sequence of char values, and returns a pointer to the sequence. It returns a sequence of exactly the bytes in the slice, and does not add a NUL byte. So you are taking a sequence of char values with no NUL and passing them to a C function that expects a sequence of char values that is terminated with NUL. So you get garbage in the C code.
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 centos
Outputgo env windows
OutputWhat did you do?
I run a C code with CGO.
Use
C.CBytes
to Convert Go Bytes to C Bytes.What did you expect to see?
Output:
go
What did you see instead?
Output:
goq∩
The extra char seems to be random. It will change every time I execute the binary.
Environment
I test it in different environments,
In CentOS and Windows, this error happens, while not reproducing in MacOS(M1) and Windows WSL(ubuntu).
What Should I Do
Is it a bug for CGO,
Or am I supposed to pass the length of []bytes to C function?
The text was updated successfully, but these errors were encountered: