diff options
author | Elliott Hughes <enh@google.com> | 2010-07-12 09:53:54 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-07-12 10:46:14 -0700 |
commit | 64101124267c6a0a9a12dc854bdec39cbc506259 (patch) | |
tree | 4047d43f75b68951be6893b836ad81b7c3c6a22a /include/ScopedPrimitiveArray.h | |
parent | ccbe3404e0691dab506d017550658e8e5974c83e (diff) | |
download | libcore-64101124267c6a0a9a12dc854bdec39cbc506259.zip libcore-64101124267c6a0a9a12dc854bdec39cbc506259.tar.gz libcore-64101124267c6a0a9a12dc854bdec39cbc506259.tar.bz2 |
Add null-pointer checking to ScopedPrimitiveArray.
This style worked well for ScopedUtfChars. It moves null-pointer checking
inside the class, thereby encouraging us to remember to check for the
unlikely out-of-memory failures too.
I've also broken up some tests that were trying to check multiple scoped
arrays at once. This idiom was broken because as soon as there's a pending
exception, it's a JNI error to even attempt to set up the next scoped
primitive array. In the absence of C++ exceptions, we have to check these
one by one.
Change-Id: I2f4b397ae2873597e309d86fcc5912f3fcf0f304
Diffstat (limited to 'include/ScopedPrimitiveArray.h')
-rw-r--r-- | include/ScopedPrimitiveArray.h | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/include/ScopedPrimitiveArray.h b/include/ScopedPrimitiveArray.h index b6dfd95..079e98c 100644 --- a/include/ScopedPrimitiveArray.h +++ b/include/ScopedPrimitiveArray.h @@ -28,7 +28,11 @@ public: \ Scoped ## NAME ## ArrayRO(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ - mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ + if (mJavaArray == NULL) { \ + jniThrowNullPointerException(mEnv, NULL); \ + } else { \ + mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ + } \ } \ ~Scoped ## NAME ## ArrayRO() { \ if (mRawArray) { \ @@ -66,7 +70,11 @@ INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short); public: \ Scoped ## NAME ## ArrayRW(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ - mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ + if (mJavaArray == NULL) { \ + jniThrowNullPointerException(mEnv, NULL); \ + } else { \ + mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ + } \ } \ ~Scoped ## NAME ## ArrayRW() { \ if (mRawArray) { \ |