Skip to content

Commit 0472517

Browse files
jkdsnot-fl3
authored andcommitted
android: ndk_utils::get_utf_str!() macro leaks memory. Free the string after use.
1 parent 613e995 commit 0472517

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/native/android.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl crate::native::Clipboard for AndroidClipboard {
344344
return None;
345345
}
346346

347-
let text = ndk_utils::get_utf_str!(env, text).to_string();
347+
let text = ndk_utils::get_utf_str!(env, text);
348348
Some(text)
349349
}
350350
}

src/native/android/ndk_utils.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ macro_rules! call_bool_method {
7373
#[macro_export]
7474
macro_rules! get_utf_str {
7575
($env:expr, $obj:expr) => {{
76-
let string = (**$env).GetStringUTFChars.unwrap()($env, $obj, std::ptr::null_mut());
77-
let string = std::ffi::CStr::from_ptr(string);
78-
string.to_str().unwrap()
76+
let cstr_dat = (**$env).GetStringUTFChars.unwrap()($env, $obj, std::ptr::null_mut());
77+
let string = std::ffi::CStr::from_ptr(cstr_dat)
78+
.to_str()
79+
.unwrap()
80+
.to_string();
81+
(**$env).ReleaseStringUTFChars.unwrap()($env, $obj, cstr_dat);
82+
string
7983
}};
8084
}
8185

0 commit comments

Comments
 (0)