]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: fix _cgo_allocate(0)
authorRuss Cox <rsc@golang.org>
Tue, 7 Oct 2014 20:27:40 +0000 (16:27 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 7 Oct 2014 20:27:40 +0000 (16:27 -0400)
Fixes a SWIG bug reported off-list.

LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/155990043

misc/cgo/test/callback_c_gc.c
misc/cgo/test/callback_c_gccgo.c
src/runtime/cgocallback.go

index 32bfed0c02ca0cc5d22403f87e40f511730aaf8e..28a62c6dbc610a7cc86de5569005d66c27463427 100644 (file)
@@ -39,6 +39,16 @@ callCgoAllocate(void)
        int i;
        struct { size_t n; void *ret; } a;
        List *l, *head, **tail;
+
+       // Make sure this doesn't crash.
+       // And make sure it returns non-nil.
+       a.n = 0;
+       a.ret = 0;
+       crosscall2(_cgo_allocate, &a, sizeof a);
+       if(a.ret == 0) {
+               fprintf(stderr, "callCgoAllocate: alloc 0 returned nil\n");
+               exit(2);
+       }
        
        head = 0;
        tail = &head;
index d92dca00937d8f1ed5627659189852fefcd7655a..d367b7b68b46a923d7fc4f285af3439ddf60797d 100644 (file)
@@ -35,6 +35,13 @@ callCgoAllocate(void)
        int i;
        List *l, *head, **tail;
        
+       // Make sure this doesn't crash.
+       // And make sure it returns non-nil.
+       if(_cgo_allocate(0) == 0) {
+               fprintf(stderr, "callCgoAllocate: alloc 0 returned nil\n");
+               exit(2);
+       }
+
        head = 0;
        tail = &head;
        for(i=0; i<100; i++) {
index 1e1b576072d3b1c58082e09070ec69c504a880cd..2c89143208ae05c862e18afd8a6e8a729d898cd6 100644 (file)
@@ -21,6 +21,9 @@ import "unsafe"
 // Either we need to add types or we need to stop using it.
 
 func _cgo_allocate_internal(len uintptr) unsafe.Pointer {
+       if len == 0 {
+               len = 1
+       }
        ret := unsafe.Pointer(&make([]unsafe.Pointer, (len+ptrSize-1)/ptrSize)[0])
        c := new(cgomal)
        c.alloc = ret