aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2013-09-16 14:04:55 -0400
committerRuss Cox <rsc@golang.org>2013-09-16 14:04:55 -0400
commit647eaed93b2aba07d4d4d032d5f58bf6dbc06e5a (patch)
treee2cbd94e66668dea4773f4edb2f5cddeb154035e
parentb2794a1c2ed8c74563cf28d9e4a9b3f1db43ef1f (diff)
downloadgo-647eaed93b2aba07d4d4d032d5f58bf6dbc06e5a.tar.xz
cmd/cgo: allow C.malloc(0) always
Because we can, and because it otherwise might crash the program if we think we're out of memory. Fixes #6390. R=golang-dev, iant, minux.ma CC=golang-dev https://golang.org/cl/13345048
-rw-r--r--misc/cgo/test/cgo_test.go1
-rw-r--r--src/cmd/cgo/out.go2
-rw-r--r--src/pkg/runtime/cgo/gcc_util.c2
3 files changed, 5 insertions, 0 deletions
diff --git a/misc/cgo/test/cgo_test.go b/misc/cgo/test/cgo_test.go
index e36f93597c..38151abca8 100644
--- a/misc/cgo/test/cgo_test.go
+++ b/misc/cgo/test/cgo_test.go
@@ -46,5 +46,6 @@ func Test3250(t *testing.T) { test3250(t) }
func TestCallbackStack(t *testing.T) { testCallbackStack(t) }
func TestFpVar(t *testing.T) { testFpVar(t) }
func Test4339(t *testing.T) { test4339(t) }
+func Test6390(t *testing.T) { test6390(t) }
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go
index efa55a335b..9cf8dc55be 100644
--- a/src/cmd/cgo/out.go
+++ b/src/cmd/cgo/out.go
@@ -1225,6 +1225,8 @@ Slice GoBytes(char *p, int32_t n) {
extern void runtime_throw(const char *):
void *Cmalloc(size_t n) {
void *p = malloc(n);
+ if(p == NULL && n == 0)
+ p = malloc(1);
if(p == NULL)
runtime_throw("runtime: C malloc failed");
return p;
diff --git a/src/pkg/runtime/cgo/gcc_util.c b/src/pkg/runtime/cgo/gcc_util.c
index 20913d7369..143734e94b 100644
--- a/src/pkg/runtime/cgo/gcc_util.c
+++ b/src/pkg/runtime/cgo/gcc_util.c
@@ -14,6 +14,8 @@ x_cgo_malloc(void *p)
} *a = p;
a->ret = malloc(a->n);
+ if(a->ret == NULL && a->n == 0)
+ a->ret = malloc(1);
}
/* Stub for calling free from Go */