From 05960876dff6a5b686821eed8f7ae7cef5af4f50 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 26 May 2010 17:45:07 -0700 Subject: 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 --- include/ScopedUtfChars.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'include') 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() { -- cgit v1.1