diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-05 02:17:50 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-06-05 02:18:12 +0200 |
commit | dea60e13c0f32d5e1f2b6f36e5657ff7350894ce (patch) | |
tree | 886380b6b3141e77e379afd9e09a17a633f5fda3 /app/tools/libwg-go | |
parent | 40a30d997d256cf9ea86163f2eb16c69fc1f9cfd (diff) |
libwg-go: more efficient and safer string passing
It was unclear when the Go string was actually freed.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/tools/libwg-go')
-rw-r--r-- | app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go | 4 | ||||
-rw-r--r-- | app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/jni.c | 13 |
2 files changed, 7 insertions, 10 deletions
diff --git a/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go index 19063eec..8ef5f21b 100644 --- a/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go +++ b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/api-android.go @@ -161,8 +161,8 @@ func wgGetSocketV6(tunnelHandle int32) int32 { } //export wgVersion -func wgVersion() string { - return WireGuardGoVersion +func wgVersion() *C.char { + return C.CString(WireGuardGoVersion) } func main() {} diff --git a/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/jni.c b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/jni.c index 7cfadcd2..102c363f 100644 --- a/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/jni.c +++ b/app/tools/libwg-go/src/git.zx2c4.com/wireguard-go/jni.c @@ -12,7 +12,7 @@ extern int wgTurnOn(struct go_string ifname, int tun_fd, struct go_string settin extern void wgTurnOff(int handle); extern int wgGetSocketV4(int handle); extern int wgGetSocketV6(int handle); -extern struct go_string wgVersion(); +extern char *wgVersion(); JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgTurnOn(JNIEnv *env, jclass c, jstring ifname, jint tun_fd, jstring settings) { @@ -50,13 +50,10 @@ JNIEXPORT jint JNICALL Java_com_wireguard_android_backend_GoBackend_wgGetSocketV JNIEXPORT jstring JNICALL Java_com_wireguard_android_backend_GoBackend_wgVersion(JNIEnv *env, jclass c) { jstring ret; - struct go_string s = wgVersion(); - char *cstr = malloc(s.n + 1); - if (!cstr) + char *version = wgVersion(); + if (!version) return NULL; - memcpy(cstr, s.str, s.n); - cstr[s.n] = '\0'; - ret = (*env)->NewStringUTF(env, cstr); - free(cstr); + ret = (*env)->NewStringUTF(env, version); + free(version); return ret; } |