]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: throw if C.malloc returns NULL in C.CString or C.CBytes
authorTobias Klauser <tklauser@distanz.ch>
Sun, 11 Apr 2021 12:49:38 +0000 (14:49 +0200)
committerTobias Klauser <tobias.klauser@gmail.com>
Mon, 12 Apr 2021 05:12:42 +0000 (05:12 +0000)
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>
src/cmd/cgo/out.go

index d0a7369c94f352ece4d13754ecc527f5b817707d..2d8d692622a9e27ddbaf7561e9a5ce9d7f175c82 100644 (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)