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

cmd/cgo: cannot convert from signed char *_Ctype_schar to char *_Ctype_char #26655

Closed
brunoamancio opened this issue Jul 28, 2018 · 4 comments

Comments

@brunoamancio
Copy link

commented Jul 28, 2018

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

go version go1.10.3 linux/arm

Does this issue reproduce with the latest release?

Yes

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

Linux raspberrypi 4.5.0-00185-g3bb556b #3 SMP Wed Aug 16 15:56:01 CST 2017 armv7l GNU/Linux

go env

GOARCH="arm"
GOBIN=""
GOCACHE="/home/pi/.cache/go-build"
GOEXE=""
GOHOSTARCH="arm"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pi/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_arm"
GCCGO="gccgo"
GOARM="6"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build986530035=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Myfile.go

package main

// #cgo CFLAGS: -g
// #include <stdlib.h>
// #include "cLogic.h"
import "C"
import (
    "fmt"
    "unsafe"
)    

func main() {
    myString := "DUMMY"
    cMyString := C.CString(myString)
    defer C.free(unsafe.Pointer(cMyString))

    cMyInt := C.int(10)

    cResult := C.MyCFunction(cMyString, cMyInt) // Result is type *_Ctype_schar (int8_t *)
    goResult := C.GoString(cResult)
    fmt.Println("GoResult: " + goResult + "\n")
}

File cLogic.h

#include <stdint.h>

int8_t *MyCFunction(char *myString, int myInt);

File cLogic.c

#include <stdint.h>

int8_t *MyCFunction(char *myString, int myInt){
    return "this is test";
}

All files are in a directory called main. I build it by executing this command in the parent directory:

go build -o myExecutable ./main

What did you expect to see?

I expected it to build. It builds fine on my desktop with ubuntu and "go version go1.10.3 linux/amd64"

What did you see instead?

I get an error in the line

goResult := C.GoString(cResult)

cannot use cResult (type *_Ctype_schar) as type *_Ctype_char in argument to _Cfunc_GoString

@brunoamancio brunoamancio changed the title cmd/cgo: Cannot convert type in the ARM version of go cgo: Cannot convert type in the ARM version of go Jul 28, 2018

@odeke-em

This comment has been minimized.

Copy link
Member

commented Jul 28, 2018

Hello there @brunoamancio, thank you for reporting this issue!

Just for providing a diversity of repros:

On my Macbook Pro Darwin at go tip 263382eb0a

$ go build -o myExecutable .
# _/Users/emmanuelodeke/Desktop/openSrc/bugs/golang/26655
cLogic.c:4:12: warning: returning 'char [13]' from a function with result type 'int8_t *' (aka 'signed char *') converts between pointers to integer types with different sign [-Wpointer-sign]
# _/Users/emmanuelodeke/Desktop/openSrc/bugs/golang/26655

On my Linux machine

I get a similar error about the failed conversion from a signed char * to char * even on Ubuntu Linux linux/amd64 at tip 263382eb0a and even on Go1.8, Go1.9, Go1.10
where from Go1.8 to Go1.9 the error was

# _/home/emmanuel/Desktop/opensrc/bugs/golang/26655
./main.go:20: cannot use cResult (type *C.int8_t) as type *C.char in argument to _Cfunc_GoString

while for Go1.10 and above, it is

# _/home/emmanuel/Desktop/opensrc/bugs/golang/26655
./main.go:20: cannot use cResult (type *_Ctype_schar) as type *_Ctype_char in argument to _Cfunc_GoString

@odeke-em odeke-em changed the title cgo: Cannot convert type in the ARM version of go cmd/cgo: cannot convert from signed char *_Ctype_schar to char *_Ctype_char Jul 28, 2018

@odeke-em

This comment has been minimized.

Copy link
Member

commented Jul 28, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Aug 1, 2018

Your MyCFunction returns int8_t*. C.GoString expects char*. Go is more strictly typed than C, and Go does not permit this. I don't see a bug here.

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Aug 1, 2018

@gopherbot

This comment has been minimized.

Copy link

commented Sep 1, 2018

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@gopherbot gopherbot closed this Sep 1, 2018

@golang golang locked and limited conversation to collaborators Sep 1, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants
You can’t perform that action at this time.