Skip to content

Commit

Permalink
runtime: fix _cgo_allocate(0)
Browse files Browse the repository at this point in the history
Fixes a SWIG bug reported off-list.

LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/155990043
  • Loading branch information
rsc committed Oct 7, 2014
1 parent 87f51f1 commit 6852047
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions misc/cgo/test/callback_c_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions misc/cgo/test/callback_c_gccgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/cgocallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6852047

Please sign in to comment.