Skip to content

Commit 78ecc7d

Browse files
committed
Eliminate C-based memcpy for stream handling
1 parent 26ceede commit 78ecc7d

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

compiler_yara37.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void freeCallback(char*, void*);
1818
*/
1919
import "C"
2020
import (
21+
"reflect"
2122
"unsafe"
2223
)
2324

@@ -49,9 +50,15 @@ func includeCallback(name, filename, namespace *C.char, userData unsafe.Pointer)
4950
if buf := callbackFunc(
5051
C.GoString(name), C.GoString(filename), C.GoString(namespace),
5152
); buf != nil {
52-
outbuf := C.calloc(1, C.size_t(len(buf)+1))
53-
C.memcpy(outbuf, unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
54-
return (*C.char)(outbuf)
53+
ptr := C.calloc(1, C.size_t(len(buf)+1))
54+
if ptr == nil {
55+
return nil
56+
}
57+
outbuf := make([]byte, 0)
58+
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&outbuf))
59+
hdr.Data, hdr.Len = uintptr(ptr), len(buf)+1
60+
copy(outbuf, buf)
61+
return (*C.char)(ptr)
5562
}
5663
return nil
5764
}

0 commit comments

Comments
 (0)