From 3f83b42db574af5a10f0ed959e30056889227414 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 20 Aug 2024 18:12:56 +1000 Subject: [PATCH] FIX: Address Mac CGO Crash (#3590) * Copy request to Go memory * Update changelog.mdx * Update v2/pkg/assetserver/webview/responsewriter_darwin.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Fix import --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .../webview/responsewriter_darwin.go | 24 +++++++++++++++---- website/src/pages/changelog.mdx | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/v2/pkg/assetserver/webview/responsewriter_darwin.go b/v2/pkg/assetserver/webview/responsewriter_darwin.go index 77de3c455..a3c73b6f1 100644 --- a/v2/pkg/assetserver/webview/responsewriter_darwin.go +++ b/v2/pkg/assetserver/webview/responsewriter_darwin.go @@ -69,6 +69,7 @@ import "C" import ( "encoding/json" + "fmt" "net/http" "unsafe" ) @@ -98,16 +99,31 @@ func (rw *responseWriter) Write(buf []byte) (int, error) { rw.WriteHeader(http.StatusOK) - var content unsafe.Pointer var contentLen int if buf != nil { - content = unsafe.Pointer(&buf[0]) contentLen = len(buf) } - if !C.URLSchemeTaskDidReceiveData(rw.r.task, content, C.int(contentLen)) { - return 0, errRequestStopped + if contentLen > 0 { + // Create a C array to hold the data + cBuf := C.malloc(C.size_t(contentLen)) + if cBuf == nil { + return 0, fmt.Errorf("memory allocation failed for %d bytes", contentLen) + } + defer C.free(cBuf) + + // Copy the Go slice to the C array + C.memcpy(cBuf, unsafe.Pointer(&buf[0]), C.size_t(contentLen)) + + if !C.URLSchemeTaskDidReceiveData(rw.r.task, cBuf, C.int(contentLen)) { + return 0, errRequestStopped + } + } else { + if !C.URLSchemeTaskDidReceiveData(rw.r.task, nil, 0) { + return 0, errRequestStopped + } } + return contentLen, nil } diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index 368e41030..72eca05a5 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed +- Fixed CGO memory issue on Darwin by @leaanthony in [PR](https://github.com/wailsapp/wails/pull/3590) - Fixed an error that occurred when an author name contains a string that is not suitable for JSON. Fixed by @taiseiotsuka in [PR](https://github.com/wailsapp/wails/pull/3638) - Fixed MacOS build to use `outputfilename` from wails.json. [#3200](https://github.com/wailsapp/wails/issues/3200) - Fixed file drop events on windows. Fixed in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi @@ -24,7 +25,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 + Modified `ZoomFactor` and `IsZoomControlEnabled` options to be Windows-only options in PR[#3644](https://github.com/wailsapp/wails/pull/3644) by @levinit - Added nil check for Drag-n-Drop on Windows. Fixed by in [PR](https://github.com/wailsapp/wails/pull/3597) by @leaanthony based on the suggestion by @Alpa-1 in [#3596](https://github.com/wailsapp/wails/issues/3596). - ## v2.9.1 - 2024-06-18 ### Fixed