From 640349a6105e64eb9e5085d2afee1ab2ec67d2dc Mon Sep 17 00:00:00 2001 From: Christian Haas Date: Wed, 28 Apr 2021 22:36:26 +0200 Subject: [PATCH] replace direct use of reflect.SliceHeader with cast; avoid zero-byte allocation (#117) --- tmpl/conversions.tmpl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tmpl/conversions.tmpl b/tmpl/conversions.tmpl index 3d6ffd4..dbb953b 100644 --- a/tmpl/conversions.tmpl +++ b/tmpl/conversions.tmpl @@ -98,14 +98,13 @@ func Strs(strs ...string) (cstrs **uint8, free func()) { for i := range strs { n += len(strs[i]) } + if n == 0 { + n = 1 // avoid allocating zero bytes in case all strings are empty. + } data := C.malloc(C.size_t(n)) // Copy all the strings into data. - dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ - Data: uintptr(data), - Len: n, - Cap: n, - })) + dataSlice := (*[1 << 30]byte)(data)[:n] css := make([]*uint8, len(strs)) // Populated with pointers to each string. offset := 0 for i := range strs {