I've been playing around with Cgo and came across a strange crash. The use is case extremely simple and I have no idea what I'm doing wrong. It really looks like a bug in cgo to me but I wanted to confirm that I'm not missing something simple.
I'm on 64-bit Darwin, 10.8.3, 64-bit, Go 1.1 compiled from source.
#include <stdio.h>
#include <stdlib.h>
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef __complex float GoComplex64;
typedef __complex double GoComplex128;
typedef struct { char *p; int n; } GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; int len; int cap; } GoSlice;
extern void hello_from_c(GoString p0, GoInt p1);
void my_printf(char *s){
printf("From Go: %s\n", s);
GoString d;
d.p = "C Test";
d.n = 7;
// printf("C STRING: %s\n", d.p);
// fflush(stdout);
hello_from_c(d, 42);
}
From Go: GoTest
runtime: out of memory: cannot allocate 140733193453568-byte block (1048576 in use)
fatal error: out of memory
goroutine 1 [running]:
[fp=0x41d4700] runtime.throw(0x410b465)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/runtime/panic.c:473 +0x67
[fp=0x41d4758] runtime.mallocgc(0x7fff0000000f, 0x100000001, 0x1)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/runtime/zmalloc_darwin_amd64.c:60 +0x316
[fp=0x41d4790] makeslice1(0x40807c0, 0x8, 0x7fff0000000f, 0x41d4850)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/runtime/slice.c:61 +0x89
[fp=0x41d47c0] growslice1(0x40807c0, 0xc200000018, 0x8, 0x8, 0x7fff0000000f, ...)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/runtime/slice.c:230 +0x56
[fp=0x41d4820] runtime.appendstr()
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/runtime/slice.c:147 +0x79
[fp=0x41d48c0] fmt.(*fmt).padString(0xc20005b128, 0x4067e1d, 0x7fff00000007)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/fmt/format.go:129 +0x1ba
[fp=0x41d48f0] fmt.(*fmt).fmt_s(0xc20005b128, 0x4067e1d, 0x7fff00000007)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/fmt/format.go:272 +0x5b
[fp=0x41d4920] fmt.(*pp).fmtString(0xc20005b0d0, 0x4067e1d, 0x7fff00000007, 0x73)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/fmt/print.go:537 +0xb3
[fp=0x41d4980] fmt.(*pp).printField(0xc20005b0d0, 0x4080340, 0xc200039160, 0x73, 0x0, ...)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/fmt/print.go:799 +0x9b6
[fp=0x41d4d00] fmt.(*pp).doPrintf(0xc20005b0d0, 0x40b05b0, 0xf, 0x41d4e08, 0x2, ...)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/fmt/print.go:1111 +0x1276
[fp=0x41d4d50] fmt.Fprintf(0xc200058150, 0xc200000008, 0x40b05b0, 0xf, 0x41d4e08, ...)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/fmt/print.go:214 +0x80
[fp=0x41d4da8] fmt.Printf(0x40b05b0, 0xf, 0x41d4e08, 0x2, 0x2, ...)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/fmt/print.go:223 +0x8c
[fp=0x41d4e30] main.hello_from_c(0x4067e1d, 0x7fff00000007, 0x2a)
play/_obj/play.cgo1.go:16 +0xdd
----- stack segment boundary -----
[fp=0x41d4ee0] runtime.cgocallbackg(0x7fff5fbff870, 0x7fff5fbff8f8, 0x18)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/runtime/cgocall.c:260 +0x118
[fp=0x41d4f00] runtime.cgocallback_gofunc()
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/runtime/asm_amd64.s:635 +0x71
[fp=0x41d4f08] return()
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/runtime/asm_amd64.s:508
[fp=0x41d4f50] runtime.cgocall(0x4000c20, 0x41d4f68)
/Users/dimitarvelitchkov/Documents/Projects/GoProjects/GoSrc/go/src/pkg/runtime/cgocall.c:162 +0x128
[fp=0x41d4f68] main._Cfunc_my_printf(0x4400000)
play/_obj/_cgo_defun.c:52 +0x2f
[fp=0x41d4f90] main.main()
play/_obj/play.cgo1.go:28 +0x64
[fp=0x41d4fb8] runtime.main()