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: compiler complains about C.free(*C.char) but is happy with defer C.free(*C.char) #17562

Closed
fanyang01 opened this issue Oct 24, 2016 · 2 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@fanyang01
Copy link

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

go version go1.7.3 linux/amd64

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

GOARCH="amd64"
GOBIN="/home/fan/go/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/fan/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build668013101=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

package main

// #include <stdlib.h>
// #include <string.h>
//
// const char *s = "hello";
//
// char *cstring(int *len) {
//     *len = strlen(s);
//     char *buf = (char *)malloc(*len);
//     return strncpy(buf, s, *len);
// }
import "C"
import "fmt"

func main() {
    fmt.Println(f())
}

func f() string {
    var (
        buf *C.char
        len C.int
    )

    if buf = C.cstring(&len); buf == nil {
        return ""
    }
    // defer C.free(unsafe.Pointer(buf))
    defer C.free(buf)
    return C.GoStringN(buf, len)
}

func g() string {
    var (
        buf *C.char
        len C.int
    )

    if buf = C.cstring(&len); buf == nil {
        return ""
    }
    s := C.GoStringN(buf, len)

    // C.free(unsafe.Pointer(buf))
    C.free(buf)

    return s
}

What did you expect to see?

Consistent behavior.

What did you see instead?

  • Compile error for C.free(*C.char):
./test.go:46: cannot use buf (type *C.char) as type unsafe.Pointer in argument to _cgoCheckPointer0
  • Runtime error for defer C.free(*C.char):
panic: interface conversion: interface {} is *main._Ctype_char, not unsafe.Pointer
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 24, 2016
@quentinmit quentinmit added this to the Go1.8 milestone Oct 24, 2016
@quentinmit
Copy link
Contributor

/cc @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

Thanks for providing a standalone test case. This is a dup of #16591, already fixed for 1.8. With 1.8 I get

foo8.go:30: cannot use buf (type *C.char) as type unsafe.Pointer in argument to func literal
foo8.go:46: cannot use buf (type *C.char) as type unsafe.Pointer in argument to func literal

@golang golang locked and limited conversation to collaborators Oct 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants