Skip to content

CGO C.Bytes Not Work As Expected #49670

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

Closed
LawyZheng opened this issue Nov 19, 2021 · 1 comment
Closed

CGO C.Bytes Not Work As Expected #49670

LawyZheng opened this issue Nov 19, 2021 · 1 comment

Comments

@LawyZheng
Copy link

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

$ go version  1.17.3

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env centos Output
$ go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/home/xxxxx/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/xxxxxx/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/xxxxxx/cgo-test/go.mod"
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-build2463505218=/tmp/go-build -gno-record-gcc-switches"

go env windows Output
$ go env

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.

package main

/*
#include <stdio.h>

static void SayHello(const void* s) {
    puts(s);
}
*/
import "C"

func main() {
    b := []byte("go")
    C.SayHello(C.CBytes(b))
}

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?

@ianlancetaylor
Copy link
Contributor

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.

@golang golang locked and limited conversation to collaborators Nov 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants