summaryrefslogtreecommitdiff
path: root/src/cmd/cgo
diff options
context:
space:
mode:
authorPeter Collingbourne <pcc@google.com>2014-04-26 22:16:38 -0700
committerPeter Collingbourne <pcc@google.com>2014-04-26 22:16:38 -0700
commit19ad7ca20214d25fe390f89ec97a64c2615c1d68 (patch)
treee3eb5940c51c601876cd5d50b2990b48f9d0714e /src/cmd/cgo
parentd9632f8c2df2c70132ba4668e7ed24533589609e (diff)
downloadgo-19ad7ca20214d25fe390f89ec97a64c2615c1d68.tar.gz
cmd/cgo: fix C.CString for strings containing null terminators under gccgo
Previously we used strndup(3) to implement C.CString for gccgo. This is wrong because strndup assumes the string to be null terminated, and stops at the first null terminator. Instead, use malloc and memmove to create a copy of the string, as we do in the gc implementation. LGTM=iant R=iant CC=golang-codereviews https://codereview.appspot.com/96790047 Committer: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r--src/cmd/cgo/out.go5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go
index 0c32e4ca1..3f04f1361 100644
--- a/src/cmd/cgo/out.go
+++ b/src/cmd/cgo/out.go
@@ -1225,7 +1225,10 @@ 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);
const char *_cgoPREFIX_Cfunc_CString(struct __go_string s) {
- return strndup((const char*)s.__data, s.__length);
+ char *p = malloc(s.__length+1);
+ memmove(p, s.__data, s.__length);
+ p[s.__length] = 0;
+ return p;
}
struct __go_string _cgoPREFIX_Cfunc_GoString(char *p) {