From 16cd770e0668a410a511680b2ac1412e554bd27b Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 11 Apr 2021 14:49:38 +0200 Subject: [PATCH] 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 Run-TryBot: Tobias Klauser TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- src/cmd/cgo/out.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index d0a7369c94..2d8d692622 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -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) -- 2.50.0