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

Side by Side Diff: base/android/jni_string.cc

Issue 1828193002: DCHECK that jstring to C++ string conversions don't pass in null. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix all the things Created 4 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/android/jni_string.h" 5 #include "base/android/jni_string.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 10
11 namespace { 11 namespace {
12 12
13 // Internal version that does not use a scoped local pointer. 13 // Internal version that does not use a scoped local pointer.
14 jstring ConvertUTF16ToJavaStringImpl(JNIEnv* env, 14 jstring ConvertUTF16ToJavaStringImpl(JNIEnv* env,
15 const base::StringPiece16& str) { 15 const base::StringPiece16& str) {
16 jstring result = env->NewString(str.data(), str.length()); 16 jstring result = env->NewString(str.data(), str.length());
17 base::android::CheckException(env); 17 base::android::CheckException(env);
18 return result; 18 return result;
19 } 19 }
20 20
21 } // namespace 21 } // namespace
22 22
23 namespace base { 23 namespace base {
24 namespace android { 24 namespace android {
25 25
26 void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) { 26 void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) {
27 if (!str) { 27 DCHECK(str);
28 LOG(WARNING) << "ConvertJavaStringToUTF8 called with null string.";
29 result->clear();
30 return;
31 }
32 const jsize length = env->GetStringLength(str); 28 const jsize length = env->GetStringLength(str);
33 if (!length) { 29 if (!length) {
34 result->clear(); 30 result->clear();
35 CheckException(env); 31 CheckException(env);
36 return; 32 return;
37 } 33 }
38 // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so 34 // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so
39 // instead get the String in UTF16 and convert using chromium's conversion 35 // instead get the String in UTF16 and convert using chromium's conversion
40 // function that yields plain (non Java-modified) UTF8. 36 // function that yields plain (non Java-modified) UTF8.
41 const jchar* chars = env->GetStringChars(str, NULL); 37 const jchar* chars = env->GetStringChars(str, NULL);
(...skipping 25 matching lines...) Expand all
67 // Further, Dalvik requires the string passed into NewStringUTF() to come from 63 // Further, Dalvik requires the string passed into NewStringUTF() to come from
68 // a trusted source. We can't guarantee that all UTF8 will be sanitized before 64 // a trusted source. We can't guarantee that all UTF8 will be sanitized before
69 // it gets here, so constructing via UTF16 side-steps this issue. 65 // it gets here, so constructing via UTF16 side-steps this issue.
70 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be 66 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be
71 // a significant performance hit by doing it this way). 67 // a significant performance hit by doing it this way).
72 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl( 68 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl(
73 env, UTF8ToUTF16(str))); 69 env, UTF8ToUTF16(str)));
74 } 70 }
75 71
76 void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) { 72 void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) {
77 if (!str) { 73 DCHECK(str);
78 LOG(WARNING) << "ConvertJavaStringToUTF16 called with null string.";
79 result->clear();
80 return;
81 }
82 const jsize length = env->GetStringLength(str); 74 const jsize length = env->GetStringLength(str);
83 if (!length) { 75 if (!length) {
84 result->clear(); 76 result->clear();
85 CheckException(env); 77 CheckException(env);
86 return; 78 return;
87 } 79 }
88 const jchar* chars = env->GetStringChars(str, NULL); 80 const jchar* chars = env->GetStringChars(str, NULL);
89 DCHECK(chars); 81 DCHECK(chars);
90 // GetStringChars isn't required to NULL-terminate the strings 82 // GetStringChars isn't required to NULL-terminate the strings
91 // it returns, so the length must be explicitly checked. 83 // it returns, so the length must be explicitly checked.
(...skipping 18 matching lines...) Expand all
110 102
111 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString( 103 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString(
112 JNIEnv* env, 104 JNIEnv* env,
113 const base::StringPiece16& str) { 105 const base::StringPiece16& str) {
114 return ScopedJavaLocalRef<jstring>(env, 106 return ScopedJavaLocalRef<jstring>(env,
115 ConvertUTF16ToJavaStringImpl(env, str)); 107 ConvertUTF16ToJavaStringImpl(env, str));
116 } 108 }
117 109
118 } // namespace android 110 } // namespace android
119 } // namespace base 111 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698