cmd/cgo: throw if C.malloc returns NULL in C.CString or C.CBytes

Change-Id: Iea07b7f3e64f5938cfb1cd1c07bdce4adf8e4470
Reviewed-on: https://go-review.googlesource.com/c/go/+/308992
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Tobias Klauser 2021-04-11 14:49:38 +02:00 committed by Tobias Klauser
parent 954bd8203b
commit 16cd770e06

View file

@ -1717,8 +1717,12 @@ typedef struct __go_open_array {
struct __go_string __go_byte_array_to_string(const void* p, intgo len);
struct __go_open_array __go_string_to_byte_array (struct __go_string str);
extern void runtime_throw(const char *);
const char *_cgoPREFIX_Cfunc_CString(struct __go_string s) {
char *p = malloc(s.__length+1);
if(p == NULL)
runtime_throw("runtime: C malloc failed");
memmove(p, s.__data, s.__length);
p[s.__length] = 0;
return p;
@ -1726,6 +1730,8 @@ const char *_cgoPREFIX_Cfunc_CString(struct __go_string s) {
void *_cgoPREFIX_Cfunc_CBytes(struct __go_open_array b) {
char *p = malloc(b.__count);
if(p == NULL)
runtime_throw("runtime: C malloc failed");
memmove(p, b.__values, b.__count);
return p;
}
@ -1744,7 +1750,6 @@ Slice _cgoPREFIX_Cfunc_GoBytes(char *p, int32_t n) {
return __go_string_to_byte_array(s);
}
extern void runtime_throw(const char *);
void *_cgoPREFIX_Cfunc__CMalloc(size_t n) {
void *p = malloc(n);
if(p == NULL && n == 0)