diff options
author | Elliott Hughes <enh@google.com> | 2010-05-26 17:45:07 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-05-26 23:52:54 -0700 |
commit | 05960876dff6a5b686821eed8f7ae7cef5af4f50 (patch) | |
tree | 56403ecb87974f4f17b02d996ed828cb74054118 /include/ScopedUtfChars.h | |
parent | 2be0ae9c8abc05d1c94c8bb170503ee2feae1866 (diff) | |
download | libcore-05960876dff6a5b686821eed8f7ae7cef5af4f50.zip libcore-05960876dff6a5b686821eed8f7ae7cef5af4f50.tar.gz libcore-05960876dff6a5b686821eed8f7ae7cef5af4f50.tar.bz2 |
Enhance ScopedUtfChars to include the null check most callers were missing.
Also switch most non-users over to ScopedUtfChars.
Also ensure all users check that ScopedUtfChars was successful in getting the
chars.
Also rewrite ObjectInputStream and ObjectOutputStream without duplication.
Change-Id: I929d00fe3ff50b303cba4a2cf2269355e9fef5f9
Diffstat (limited to 'include/ScopedUtfChars.h')
-rw-r--r-- | include/ScopedUtfChars.h | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/include/ScopedUtfChars.h b/include/ScopedUtfChars.h index bfe9bb9..3051d3b 100644 --- a/include/ScopedUtfChars.h +++ b/include/ScopedUtfChars.h @@ -20,12 +20,25 @@ #include "JNIHelp.h" // A smart pointer that provides read-only access to a Java string's UTF chars. +// Unlike GetStringUTFChars, we throw NullPointerException rather than abort if +// passed a null jstring, and c_str will return NULL. +// This makes the correct idiom very simple: +// +// ScopedUtfChars name(env, javaName); +// if (name.c_str() == NULL) { +// return NULL; +// } class ScopedUtfChars { public: ScopedUtfChars(JNIEnv* env, jstring s) - : mEnv(env), mString(s), mUtfChars(NULL) + : mEnv(env), mString(s) { - mUtfChars = env->GetStringUTFChars(s, NULL); + if (s == NULL) { + mUtfChars = NULL; + jniThrowNullPointerException(env, NULL); + } else { + mUtfChars = env->GetStringUTFChars(s, NULL); + } } ~ScopedUtfChars() { |