From 9d62d5e3f0c515e45332f4ea5a914d7812313366 Mon Sep 17 00:00:00 2001
From: jkito <belter@riseup.net>
Date: Thu, 11 Jul 2024 23:54:43 +0530
Subject: [PATCH] qt: pass char * to go functions from cpp helpers

instead of converting QString to GoString struct which is
error prone, this changes SetTransport and UseLocation in
backend.go to receive a char* as arg, which is  converted
to Go string using the C.GoString helper

this fixes a bug on macOS where changing preference options
did not have any effect as the conversion of string was not
working and empty values were passed to Go functions
---
 gui/backend.go   | 11 +++++------
 gui/handlers.cpp | 10 ++++++++--
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/gui/backend.go b/gui/backend.go
index 5feb6d4b..f8626524 100644
--- a/gui/backend.go
+++ b/gui/backend.go
@@ -31,10 +31,8 @@ func SwitchOff() {
 }
 
 //export UseLocation
-func UseLocation(label string) {
-	// a bit of a hack to force the compiler to copy the string
-	// so the original C++ string will not be used as it will be changed down the line
-	location := string([]byte(label))
+func UseLocation(label *C.char) {
+	location := C.GoString(label)
 	backend.UseLocation(location)
 }
 
@@ -44,8 +42,9 @@ func UseAutomaticGateway() {
 }
 
 //export SetTransport
-func SetTransport(transport string) {
-	backend.SetTransport(string(transport))
+func SetTransport(transport *C.char) {
+	tp := C.GoString(transport)
+	backend.SetTransport(tp)
 }
 
 //export GetTransport
diff --git a/gui/handlers.cpp b/gui/handlers.cpp
index 47c90255..bc131520 100644
--- a/gui/handlers.cpp
+++ b/gui/handlers.cpp
@@ -48,7 +48,10 @@ void Backend::donateSeen()
 
 void Backend::useLocation(QString label)
 {
-    UseLocation(toGoStr(label));
+    QByteArray loc = label.toUtf8();
+    char *c = loc.data();
+
+    UseLocation(c);
 }
 
 void Backend::useAutomaticGateway()
@@ -58,7 +61,10 @@ void Backend::useAutomaticGateway()
 
 void Backend::setTransport(QString transport)
 {
-    SetTransport(toGoStr(transport));
+    QByteArray tp = transport.toUtf8();
+    char *c = tp.data();
+
+    SetTransport(c);
 }
 
 void Backend::setUDP(bool udp)
-- 
GitLab