Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2843)

Unified Diff: base/android/jni_string.cc

Issue 1263333003: [Android] Avoid unnecessary JNI calls for empty string conversions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Check exceptions... Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/android/jni_string_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/jni_string.cc
diff --git a/base/android/jni_string.cc b/base/android/jni_string.cc
index 57c2805ead39177d462daba5aa345bfd9026973d..121755ab75e46cfe6017cdae28f3870056498525 100644
--- a/base/android/jni_string.cc
+++ b/base/android/jni_string.cc
@@ -29,12 +29,18 @@ void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) {
result->clear();
return;
}
+ const jsize length = env->GetStringLength(str);
+ if (!length) {
+ result->clear();
+ CheckException(env);
+ return;
+ }
// JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so
// instead get the String in UTF16 and convert using chromium's conversion
// function that yields plain (non Java-modified) UTF8.
const jchar* chars = env->GetStringChars(str, NULL);
DCHECK(chars);
- UTF16ToUTF8(chars, env->GetStringLength(str), result);
+ UTF16ToUTF8(chars, length, result);
env->ReleaseStringChars(str, chars);
CheckException(env);
}
@@ -69,11 +75,17 @@ void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) {
result->clear();
return;
}
+ const jsize length = env->GetStringLength(str);
+ if (!length) {
+ result->clear();
+ CheckException(env);
+ return;
+ }
const jchar* chars = env->GetStringChars(str, NULL);
DCHECK(chars);
// GetStringChars isn't required to NULL-terminate the strings
// it returns, so the length must be explicitly checked.
- result->assign(chars, env->GetStringLength(str));
+ result->assign(chars, length);
env->ReleaseStringChars(str, chars);
CheckException(env);
}
« no previous file with comments | « no previous file | base/android/jni_string_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698