aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cgo
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2021-04-11 14:49:38 +0200
committerTobias Klauser <tobias.klauser@gmail.com>2021-04-12 05:12:42 +0000
commit16cd770e0668a410a511680b2ac1412e554bd27b (patch)
treef082ea9d82efbedfd2fefae40b9170012657c1f7 /src/cmd/cgo
parent954bd8203bfe3033b9794ad4ec2c82cfd6be1414 (diff)
downloadgo-16cd770e0668a410a511680b2ac1412e554bd27b.tar.gz
go-16cd770e0668a410a511680b2ac1412e554bd27b.zip
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>
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r--src/cmd/cgo/out.go7
1 files changed, 6 insertions, 1 deletions
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)