Unverified Commit 642e3008 authored by Hubert Błaszczyk's avatar Hubert Błaszczyk Committed by GitHub

Fix memory leak in JNI skString util (#1218)

Fixes - [SKIKO-1150](https://youtrack.jetbrains.com/issue/SKIKO-1150)

Based on the book *The Java Native Interface: Programmer's Guide and
Specification*
> The ReleaseStringChars call is necessary whether GetStringChars has
set *isCopy to JNI_TRUE or
JNI_FALSE. ReleaseStringChars either frees the copy or unpins the
instance,
depending upon whether GetStringChars has returned a copy or not.
parent 5138f531
...@@ -1020,8 +1020,7 @@ SkString skString(JNIEnv* env, jstring str) { ...@@ -1020,8 +1020,7 @@ SkString skString(JNIEnv* env, jstring str) {
// See https://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html#wp16542 // See https://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html#wp16542
// Instead, get data as-is (UTF-16) and convert to UTF-8 ourselves. // Instead, get data as-is (UTF-16) and convert to UTF-8 ourselves.
jsize utf16Units = env->GetStringLength(str); jsize utf16Units = env->GetStringLength(str);
jboolean isCopy; const jchar *utf16 = env->GetStringChars(str, nullptr);
const jchar *utf16 = env->GetStringChars(str, &isCopy);
// SkUTF::UTF16ToUTF8 returns empty string if there is invalid unicode characters. // SkUTF::UTF16ToUTF8 returns empty string if there is invalid unicode characters.
// Use our replacement that carefully handles invalid unicode strings. // Use our replacement that carefully handles invalid unicode strings.
...@@ -1031,9 +1030,8 @@ SkString skString(JNIEnv* env, jstring str) { ...@@ -1031,9 +1030,8 @@ SkString skString(JNIEnv* env, jstring str) {
result.resize(utf8Units); result.resize(utf8Units);
UTF16ToUTF8(result.data(), utf8Units, utf16, utf16Units); UTF16ToUTF8(result.data(), utf8Units, utf16, utf16Units);
} }
if (isCopy == JNI_TRUE) {
env->ReleaseStringChars(str, utf16); env->ReleaseStringChars(str, utf16);
}
return result; return result;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment