Skip to content

cmd/cgo: cgo treats pointer to fixed-length array as unsafe.Pointer when CC=gcc #29909

@laser

Description

@laser

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

go version go1.11.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using?

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.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-build457456374=/tmp/go-build -gno-record-gcc-switches"

gcc version:

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

clang version:

clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

What did you do?

First, you'll need some files:

demo.h

void wombat(const char (*xs)[31]);

demo.c

#include "demo.h"

void wombat(const char (*xs)[31]) {
  return;
}

main.go

package main

import "fmt"

// #cgo LDFLAGS: -L${SRCDIR} -ldemo
// #include <stdlib.h>
// #include "./demo.h"
import "C"

func main() {
	someGoBytes := make([]byte, 31)
	someCBytes := C.CBytes(someGoBytes[:])
	defer C.free(someCBytes)

	C.wombat((*[31]C.char)(someCBytes))

	fmt.Println("test complete")
}

What did you expect to see?

When running:

gcc -g -c demo.c -o demo.o \
  && gcc -g -shared -o libdemo.so demo.o \
  && CC=gcc go run main.go

I expected to see:

test complete

What did you see instead?

When running:

gcc -g -c demo.c -o demo.o \
  && gcc -g -shared -o libdemo.so demo.o \
  && CC=gcc go run main.go

I saw:

# command-line-arguments
./main.go:15:105: cannot use (*[31]_Ctype_char)(someCBytes) (type *[31]_Ctype_char) as type unsafe.Pointer in argument to func literal

Additional Notes

If I use clang instead of gcc, the program compiles and runs as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions