]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: allow C.malloc(0) always
authorRuss Cox <rsc@golang.org>
Mon, 16 Sep 2013 18:04:55 +0000 (14:04 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 16 Sep 2013 18:04:55 +0000 (14:04 -0400)
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

misc/cgo/test/cgo_test.go
src/cmd/cgo/out.go
src/pkg/runtime/cgo/gcc_util.c

index e36f93597c1dd5df39e5a5e474a0c87589a9303f..38151abca8456ea61f8bd6ffd57bc456e5887853 100644 (file)
@@ -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) }
index efa55a335b3be855863aca7ad705aafa79baad89..9cf8dc55bec3373efe29a132b153c13d5ac2bdea 100644 (file)
@@ -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;
index 20913d7369ca37505664ba990b97b504f7015e3a..143734e94b09b1fac40ebd517c9309cfe5704d8d 100644 (file)
@@ -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 */