OSDN Git Service

Add null-pointer checking to ScopedPrimitiveArray.
authorElliott Hughes <enh@google.com>
Mon, 12 Jul 2010 16:53:54 +0000 (09:53 -0700)
committerElliott Hughes <enh@google.com>
Mon, 12 Jul 2010 17:46:14 +0000 (10:46 -0700)
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

14 files changed:
include/ScopedPrimitiveArray.h
luni/src/main/native/NativeBN.cpp
luni/src/main/native/NativeBidi.cpp
luni/src/main/native/NativeConverter.cpp
luni/src/main/native/NativeCrypto.cpp
luni/src/main/native/NativeDecimalFormat.cpp
luni/src/main/native/java_io_File.cpp
luni/src/main/native/java_util_regex_Matcher.cpp
luni/src/main/native/java_util_zip_Adler32.cpp
luni/src/main/native/java_util_zip_CRC32.cpp
luni/src/main/native/java_util_zip_Inflater.cpp
luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
luni/src/main/native/org_apache_harmony_luni_util_NumberConvert.cpp
luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp

index b6dfd95..079e98c 100644 (file)
     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) { \
index ac9cd90..e742db2 100644 (file)
@@ -164,6 +164,10 @@ static jboolean NativeBN_litEndInts2bn(JNIEnv* env, jclass, jintArray arr, int l
     bn_check_top(ret);
     if (len > 0) {
         ScopedIntArrayRO scopedArray(env, arr);
+        if (scopedArray.get() == NULL) {
+            return JNI_FALSE;
+        }
+
         assert(sizeof(BN_ULONG) == sizeof(jint));
         const BN_ULONG* tmpInts = reinterpret_cast<const BN_ULONG*>(scopedArray.get());
         if ((tmpInts != NULL) && (bn_wexpand(ret, len) != NULL)) {
@@ -352,6 +356,9 @@ static jintArray NativeBN_bn2litEndInts(JNIEnv* env, jclass, BIGNUM* a) {
         return NULL;
     }
     ScopedIntArrayRW ints(env, result);
+    if (ints.get() == NULL) {
+        return NULL;
+    }
     BN_ULONG* ulongs = reinterpret_cast<BN_ULONG*>(ints.get());
     if (ulongs == NULL) {
         return NULL;
index 41a46a6..0a52f56 100644 (file)
@@ -82,8 +82,11 @@ static void NativeBidi_ubidi_setPara(JNIEnv* env, jclass, jlong ptr, jcharArray
     } else {
         data->setEmbeddingLevels(NULL);
     }
-    UErrorCode err = U_ZERO_ERROR;
     ScopedCharArrayRO chars(env, text);
+    if (chars.get() == NULL) {
+        return;
+    }
+    UErrorCode err = U_ZERO_ERROR;
     ubidi_setPara(data->uBiDi(), chars.get(), length, paraLevel, data->embeddingLevels(), &err);
     icu4jni_error(env, err);
 }
@@ -154,6 +157,10 @@ static jobjectArray NativeBidi_ubidi_getRuns(JNIEnv* env, jclass, jlong ptr) {
 
 static jintArray NativeBidi_ubidi_reorderVisual(JNIEnv* env, jclass, jbyteArray javaLevels, jint length) {
     ScopedByteArrayRO levelBytes(env, javaLevels);
+    if (levelBytes.get() == NULL) {
+        return NULL;
+    }
+
     const UBiDiLevel* levels = reinterpret_cast<const UBiDiLevel*>(levelBytes.get());
 
     UniquePtr<int[]> indexMap(new int[length]);
index 2d99383..c6b7ec3 100644 (file)
@@ -82,10 +82,19 @@ static jint NativeConverter_encode(JNIEnv* env, jclass, jlong address,
         jintArray data, jboolean flush) {
 
     UConverter* cnv = toUConverter(address);
+    if (cnv == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedCharArrayRO uSource(env, source);
+    if (uSource.get() == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedByteArrayRW uTarget(env, target);
+    if (uTarget.get() == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedIntArrayRW myData(env, data);
-    if (cnv == NULL || uSource.get() == NULL || uTarget.get() == NULL || myData.get() == NULL) {
+    if (myData.get() == NULL) {
         return U_ILLEGAL_ARGUMENT_ERROR;
     }
 
@@ -125,10 +134,19 @@ static jint NativeConverter_decode(JNIEnv* env, jclass, jlong address,
         jintArray data, jboolean flush) {
 
     UConverter* cnv = toUConverter(address);
+    if (cnv == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedByteArrayRO uSource(env, source);
+    if (uSource.get() == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedCharArrayRW uTarget(env, target);
+    if (uTarget.get() == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedIntArrayRW myData(env, data);
-    if (cnv == NULL || uSource.get() == NULL || uTarget.get() == NULL || myData.get() == NULL) {
+    if (myData.get() == NULL) {
         return U_ILLEGAL_ARGUMENT_ERROR;
     }
 
@@ -194,9 +212,15 @@ static jfloat NativeConverter_getAveBytesPerChar(JNIEnv*, jclass, jlong address)
 static jint NativeConverter_flushByteToChar(JNIEnv* env, jclass, jlong address,
         jcharArray target, jint targetEnd, jintArray data) {
     UConverter* cnv = toUConverter(address);
+    if (cnv == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedCharArrayRW uTarget(env, target);
+    if (uTarget.get() == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedIntArrayRW myData(env, data);
-    if (cnv == NULL || uTarget.get() == NULL || myData.get() == NULL) {
+    if (myData.get() == NULL) {
         return U_ILLEGAL_ARGUMENT_ERROR;
     }
     jbyte source = '\0';
@@ -214,9 +238,15 @@ static jint NativeConverter_flushByteToChar(JNIEnv* env, jclass, jlong address,
 static jint NativeConverter_flushCharToByte(JNIEnv* env, jclass, jlong address,
         jbyteArray target, jint targetEnd, jintArray data) {
     UConverter* cnv = toUConverter(address);
+    if (cnv == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedByteArrayRW uTarget(env, target);
+    if (uTarget.get() == NULL) {
+        return U_ILLEGAL_ARGUMENT_ERROR;
+    }
     ScopedIntArrayRW myData(env, data);
-    if (cnv == NULL || uTarget.get() == NULL || myData.get() == NULL) {
+    if (myData.get() == NULL) {
         return U_ILLEGAL_ARGUMENT_ERROR;
     }
     jchar source = '\0';
index 7f016fd..c3c4a91 100644 (file)
@@ -367,6 +367,9 @@ static BIGNUM* arrayToBignum(JNIEnv* env, jbyteArray source) {
     // LOGD("Entering arrayToBignum()");
 
     ScopedByteArrayRO sourceBytes(env, source);
+    if (sourceBytes.get() == NULL) {
+        return NULL;
+    }
     return BN_bin2bn((unsigned char*) sourceBytes.get(), sourceBytes.size(), NULL);
 }
 
@@ -584,6 +587,9 @@ static jint NativeCrypto_EVP_DigestFinal(JNIEnv* env, jclass, EVP_MD_CTX* ctx,
     int result = -1;
 
     ScopedByteArrayRW hashBytes(env, hash);
+    if (hashBytes.get() == NULL) {
+        return -1;
+    }
     EVP_DigestFinal(ctx, (unsigned char*) (hashBytes.get() + offset), (unsigned int*)&result);
 
     throwExceptionIfNecessary(env, "NativeCrypto_EVP_DigestFinal");
@@ -668,6 +674,9 @@ static void NativeCrypto_EVP_DigestUpdate(JNIEnv* env, jclass, EVP_MD_CTX* ctx,
     }
 
     ScopedByteArrayRO bufferBytes(env, buffer);
+    if (bufferBytes.get() == NULL) {
+        return;
+    }
     EVP_DigestUpdate(ctx, (unsigned char*) (bufferBytes.get() + offset), length);
 
     throwExceptionIfNecessary(env, "NativeCrypto_EVP_DigestUpdate");
@@ -714,6 +723,9 @@ static void NativeCrypto_EVP_VerifyUpdate(JNIEnv* env, jclass, EVP_MD_CTX* ctx,
     }
 
     ScopedByteArrayRO bufferBytes(env, buffer);
+    if (bufferBytes.get() == NULL) {
+        return;
+    }
     EVP_VerifyUpdate(ctx, (unsigned char*) (bufferBytes.get() + offset), length);
 
     throwExceptionIfNecessary(env, "NativeCrypto_EVP_VerifyUpdate");
@@ -732,6 +744,9 @@ static int NativeCrypto_EVP_VerifyFinal(JNIEnv* env, jclass, EVP_MD_CTX* ctx, jb
     }
 
     ScopedByteArrayRO bufferBytes(env, buffer);
+    if (bufferBytes.get() == NULL) {
+        return -1;
+    }
     int result = EVP_VerifyFinal(ctx, (unsigned char*) (bufferBytes.get() + offset), length, pkey);
 
     throwExceptionIfNecessary(env, "NativeCrypto_EVP_VerifyFinal");
@@ -818,16 +833,22 @@ static int NativeCrypto_verifysignature(JNIEnv* env, jclass,
     JNI_TRACE("NativeCrypto_verifysignature msg=%p sig=%p algorithm=%p mod=%p exp%p",
               msg, sig, algorithm, mod, exp);
 
-    if (msg == NULL || sig == NULL || algorithm == NULL || mod == NULL || exp == NULL) {
-        jniThrowNullPointerException(env, NULL);
-        JNI_TRACE("NativeCrypto_verifysignature => -1");
+    ScopedByteArrayRO msgBytes(env, msg);
+    if (msgBytes.get() == NULL) {
         return -1;
     }
-
-    ScopedByteArrayRO msgBytes(env, msg);
     ScopedByteArrayRO sigBytes(env, sig);
+    if (sigBytes.get() == NULL) {
+        return -1;
+    }
     ScopedByteArrayRO modBytes(env, mod);
+    if (modBytes.get() == NULL) {
+        return -1;
+    }
     ScopedByteArrayRO expBytes(env, exp);
+    if (expBytes.get() == NULL) {
+        return -1;
+    }
 
     ScopedUtfChars algorithmChars(env, algorithm);
     if (algorithmChars.c_str() == NULL) {
@@ -856,11 +877,10 @@ static int NativeCrypto_verifysignature(JNIEnv* env, jclass,
 
 static void NativeCrypto_RAND_seed(JNIEnv* env, jclass, jbyteArray seed) {
     JNI_TRACE("NativeCrypto_RAND_seed seed=%p", seed);
-    if (seed == NULL) {
-        jniThrowNullPointerException(env, "seed == null");
+    ScopedByteArrayRO randseed(env, seed);
+    if (randseed.get() == NULL) {
         return;
     }
-    ScopedByteArrayRO randseed(env, seed);
     RAND_seed(randseed.get(), randseed.size());
 }
 
@@ -1692,13 +1712,11 @@ static void NativeCrypto_SSL_use_PrivateKey(JNIEnv* env, jclass,
         return;
     }
 
-    if (privatekey == NULL) {
-        jniThrowNullPointerException(env, "privatekey == null");
-        JNI_TRACE("ssl=%p NativeCrypto_SSL_use_PrivateKey => privatekey == null", ssl);
+    ScopedByteArrayRO buf(env, privatekey);
+    if (buf.get() == NULL) {
+        JNI_TRACE("ssl=%p NativeCrypto_SSL_use_PrivateKey => threw exception", ssl);
         return;
     }
-
-    ScopedByteArrayRO buf(env, privatekey);
     const unsigned char* tmp = (const unsigned char*) buf.get();
     Unique_PKCS8_PRIV_KEY_INFO pkcs8(d2i_PKCS8_PRIV_KEY_INFO(NULL, &tmp, buf.size()));
     if (pkcs8.get() == NULL) {
@@ -1767,6 +1785,10 @@ static void NativeCrypto_SSL_use_certificate(JNIEnv* env, jclass,
         }
 
         ScopedByteArrayRO buf(env, certificate.get());
+        if (buf.get() == NULL) {
+            JNI_TRACE("ssl=%p NativeCrypto_SSL_use_certificate => threw exception", ssl);
+            return;
+        }
         const unsigned char* tmp = (const unsigned char*) buf.get();
         certificatesX509[i].reset(d2i_X509(NULL, &tmp, buf.size()));
 
@@ -2213,7 +2235,7 @@ static jobjectArray NativeCrypto_SSL_get_certificate(JNIEnv* env, jclass, jint s
     Unique_sk_X509 chain(sk_X509_new_null());
     if (chain.get() == NULL) {
         jniThrowRuntimeException(env, "Unable to allocate local certificate chain");
-        JNI_TRACE("ssl=%p NativeCrypto_SSL_get_certificate => NULL", ssl);
+        JNI_TRACE("ssl=%p NativeCrypto_SSL_get_certificate => threw exception", ssl);
         return NULL;
     }
     sk_X509_push(chain.get(), certificate);
@@ -2401,6 +2423,10 @@ static jint NativeCrypto_SSL_read(JNIEnv* env, jclass, jint
     }
 
     ScopedByteArrayRW bytes(env, b);
+    if (bytes.get() == NULL) {
+        JNI_TRACE("ssl=%p NativeCrypto_SSL_read => threw exception", ssl);
+        return 0;
+    }
     int returnCode = 0;
     int sslErrorCode = SSL_ERROR_NONE;;
 
@@ -2585,6 +2611,10 @@ static void NativeCrypto_SSL_write(JNIEnv* env, jclass,
     }
 
     ScopedByteArrayRO bytes(env, b);
+    if (bytes.get() == NULL) {
+        JNI_TRACE("ssl=%p NativeCrypto_SSL_write => threw exception", ssl);
+        return;
+    }
     int returnCode = 0;
     int sslErrorCode = SSL_ERROR_NONE;
     int ret = sslWrite(env,
@@ -2849,6 +2879,10 @@ static jbyteArray NativeCrypto_i2d_SSL_SESSION(JNIEnv* env, jclass, jint ssl_ses
     jbyteArray bytes = env->NewByteArray(size);
     if (bytes != NULL) {
         ScopedByteArrayRW tmp(env, bytes);
+        if (tmp.get() == NULL) {
+            JNI_TRACE("ssl_session=%p NativeCrypto_i2d_SSL_SESSION => threw exception");
+            return NULL;
+        }
         unsigned char* ucp = reinterpret_cast<unsigned char*>(tmp.get());
         i2d_SSL_SESSION(ssl_session, &ucp);
     }
@@ -2862,12 +2896,12 @@ static jbyteArray NativeCrypto_i2d_SSL_SESSION(JNIEnv* env, jclass, jint ssl_ses
  */
 static jint NativeCrypto_d2i_SSL_SESSION(JNIEnv* env, jclass, jbyteArray bytes, jint size) {
     JNI_TRACE("NativeCrypto_d2i_SSL_SESSION bytes=%p size=%d", bytes, size);
-    if (bytes == NULL) {
-        JNI_TRACE("NativeCrypto_d2i_SSL_SESSION => 0");
-        return 0;
-    }
 
     ScopedByteArrayRO tmp(env, bytes);
+    if (tmp.get() == NULL) {
+        JNI_TRACE("NativeCrypto_d2i_SSL_SESSION => threw exception");
+        return 0;
+    }
     const unsigned char* ucp = reinterpret_cast<const unsigned char*>(tmp.get());
     SSL_SESSION* ssl_session = d2i_SSL_SESSION(NULL, &ucp, size);
 
index ee9fc3a..b068a45 100644 (file)
@@ -201,15 +201,16 @@ static jstring formatResult(JNIEnv* env, const UnicodeString &str, FieldPosition
 
     if (fpi != NULL) {
         int len = fpi->getData(NULL, 0);
-        jintArray iary;
+        jintArray data = NULL;
         if (len) {
-            iary = env->NewIntArray(len);
-            ScopedIntArrayRW ints(env, iary);
+            data = env->NewIntArray(len);
+            ScopedIntArrayRW ints(env, data);
+            if (ints.get() == NULL) {
+                return NULL;
+            }
             fpi->getData(ints.get(), len);
-        } else {
-            iary = NULL;
         }
-        env->CallVoidMethod(fpIter, gFPI_setData, iary);
+        env->CallVoidMethod(fpIter, gFPI_setData, data);
     }
 
     return env->NewString(str.getBuffer(), str.length());
index 8b5434e..0ea6bcc 100644 (file)
@@ -47,6 +47,9 @@ static const char* toPath(const ScopedByteArrayRO& path) {
 
 static jbyteArray java_io_File_getCanonImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return NULL;
+    }
     // The only thing this native code currently does is truncate the byte[] at
     // the first NUL.
     // TODO: this is completely pointless. we should do this in Java, or do all of getCanonicalPath in native code. (realpath(2)?)
@@ -58,11 +61,17 @@ static jbyteArray java_io_File_getCanonImpl(JNIEnv* env, jobject, jbyteArray pat
 
 static jboolean java_io_File_deleteImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return JNI_FALSE;
+    }
     return (remove(toPath(path)) == 0);
 }
 
 static bool doStat(JNIEnv* env, jbyteArray pathBytes, struct stat& sb) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return JNI_FALSE;
+    }
     return (stat(toPath(path), &sb) == 0);
 }
 
@@ -108,28 +117,35 @@ static jboolean java_io_File_isFileImpl(JNIEnv* env, jobject, jbyteArray pathByt
     return (doStat(env, pathBytes, sb) && S_ISREG(sb.st_mode));
 }
 
-static jboolean java_io_File_existsImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
+static jboolean doAccess(JNIEnv* env, jbyteArray pathBytes, int mode) {
     ScopedByteArrayRO path(env, pathBytes);
-    return (access(toPath(path), F_OK) == 0);
+    if (path.get() == NULL) {
+        return JNI_FALSE;
+    }
+    return (access(toPath(path), mode) == 0);
+}
+
+static jboolean java_io_File_existsImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
+    return doAccess(env, pathBytes, F_OK);
 }
 
 static jboolean java_io_File_canExecuteImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
-    ScopedByteArrayRO path(env, pathBytes);
-    return (access(toPath(path), X_OK) == 0);
+    return doAccess(env, pathBytes, X_OK);
 }
 
 static jboolean java_io_File_canReadImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
-    ScopedByteArrayRO path(env, pathBytes);
-    return (access(toPath(path), R_OK) == 0);
+    return doAccess(env, pathBytes, R_OK);
 }
 
 static jboolean java_io_File_canWriteImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
-    ScopedByteArrayRO path(env, pathBytes);
-    return (access(toPath(path), W_OK) == 0);
+    return doAccess(env, pathBytes, W_OK);
 }
 
 static jbyteArray java_io_File_getLinkImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return NULL;
+    }
 
     // We can't know how big a buffer readlink(2) will need, so we need to
     // loop until it says "that fit".
@@ -157,6 +173,9 @@ static jbyteArray java_io_File_getLinkImpl(JNIEnv* env, jobject, jbyteArray path
 
 static jboolean java_io_File_setLastModifiedImpl(JNIEnv* env, jobject, jbyteArray pathBytes, jlong ms) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return JNI_FALSE;
+    }
 
     // We want to preserve the access time.
     struct stat sb;
@@ -173,6 +192,10 @@ static jboolean java_io_File_setLastModifiedImpl(JNIEnv* env, jobject, jbyteArra
 
 static jboolean doChmod(JNIEnv* env, jbyteArray pathBytes, mode_t mask, bool set) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return JNI_FALSE;
+    }
+
     struct stat sb;
     if (stat(toPath(path), &sb) == -1) {
         return JNI_FALSE;
@@ -198,6 +221,10 @@ static jboolean java_io_File_setWritableImpl(JNIEnv* env, jobject, jbyteArray pa
 
 static bool doStatFs(JNIEnv* env, jbyteArray pathBytes, struct statfs& sb) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return JNI_FALSE;
+    }
+
     int rc = statfs(toPath(path), &sb);
     return (rc != -1);
 }
@@ -331,6 +358,10 @@ private:
 // to 'entries'.
 static bool readDirectory(JNIEnv* env, jbyteArray pathBytes, DirEntries& entries) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return false;
+    }
+
     ScopedReaddir dir(toPath(path));
     if (dir.isBad()) {
         return false;
@@ -370,12 +401,20 @@ static jobjectArray java_io_File_listImpl(JNIEnv* env, jobject, jbyteArray pathB
 
 static jboolean java_io_File_mkdirImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return JNI_FALSE;
+    }
+
     // On Android, we don't want default permissions to allow global access.
     return (mkdir(toPath(path), S_IRWXU) == 0);
 }
 
 static jboolean java_io_File_createNewFileImpl(JNIEnv* env, jobject, jbyteArray pathBytes) {
     ScopedByteArrayRO path(env, pathBytes);
+    if (path.get() == NULL) {
+        return JNI_FALSE;
+    }
+
     // On Android, we don't want default permissions to allow global access.
     ScopedFd fd(open(toPath(path), O_CREAT | O_EXCL, 0600));
     if (fd.get() != -1) {
@@ -392,7 +431,15 @@ static jboolean java_io_File_createNewFileImpl(JNIEnv* env, jobject, jbyteArray
 
 static jboolean java_io_File_renameToImpl(JNIEnv* env, jobject, jbyteArray oldPathBytes, jbyteArray newPathBytes) {
     ScopedByteArrayRO oldPath(env, oldPathBytes);
+    if (oldPath.get() == NULL) {
+        return JNI_FALSE;
+    }
+
     ScopedByteArrayRO newPath(env, newPathBytes);
+    if (newPath.get() == NULL) {
+        return JNI_FALSE;
+    }
+
     return (rename(toPath(oldPath), toPath(newPath)) == 0);
 }
 
index f5accbd..c47a211 100644 (file)
@@ -85,6 +85,10 @@ public:
 
     void updateOffsets(jintArray javaOffsets) {
         ScopedIntArrayRW offsets(mEnv, javaOffsets);
+        if (offsets.get() == NULL) {
+            return;
+        }
+
         for (size_t i = 0, groupCount = mMatcher->groupCount(); i <= groupCount; ++i) {
             offsets[2*i + 0] = mMatcher->start(i, mStatus);
             offsets[2*i + 1] = mMatcher->end(i, mStatus);
index b326225..60eef54 100644 (file)
@@ -25,7 +25,6 @@
 static jlong Adler32_updateImpl(JNIEnv* env, jobject, jbyteArray byteArray, int off, int len, jlong crc) {
     ScopedByteArrayRO bytes(env, byteArray);
     if (bytes.get() == NULL) {
-        jniThrowNullPointerException(env, NULL);
         return 0;
     }
     return adler32((uLong) crc, (Bytef *) (bytes.get() + off), (uInt) len);
index 901f7a5..197165e 100644 (file)
@@ -25,7 +25,6 @@
 static jlong CRC32_updateImpl(JNIEnv* env, jobject, jbyteArray byteArray, int off, int len, jlong crc) {
     ScopedByteArrayRO bytes(env, byteArray);
     if (bytes.get() == NULL) {
-        jniThrowNullPointerException(env, NULL);
         return 0;
     }
     jlong result = crc32((uLong) crc, (Bytef *) (bytes.get() + off), (uInt) len);
index 63a0140..29afbbb 100644 (file)
@@ -107,7 +107,6 @@ static jint Inflater_inflateImpl(JNIEnv* env, jobject recv, jbyteArray buf, int
     jint sout = stream->stream.total_out;
     ScopedByteArrayRO out(env, buf);
     if (out.get() == NULL) {
-        jniThrowOutOfMemoryError(env, NULL);
         return -1;
     }
     stream->stream.next_out = (Bytef *) out.get() + off;
index 799da04..cd284c8 100644 (file)
@@ -889,6 +889,9 @@ static jboolean osNetworkSystem_connectWithTimeout(JNIEnv* env,
     }
 
     ScopedByteArrayRW contextBytes(env, passContext);
+    if (contextBytes.get() == NULL) {
+        return JNI_FALSE;
+    }
     selectFDSet* context = reinterpret_cast<selectFDSet*>(contextBytes.get());
     int result = 0;
     switch (step) {
@@ -1329,6 +1332,9 @@ static jint osNetworkSystem_sendDatagram(JNIEnv* env, jobject,
         jobject fd, jbyteArray data, jint offset, jint length, jint port,
         jint trafficClass, jobject inetAddress) {
     ScopedByteArrayRO bytes(env, data);
+    if (bytes.get() == NULL) {
+        return -1;
+    }
     return osNetworkSystem_sendDatagramDirect(env, NULL, fd,
             reinterpret_cast<uintptr_t>(bytes.get()), offset, length, port,
             trafficClass, inetAddress);
@@ -1357,6 +1363,9 @@ static jint osNetworkSystem_sendConnectedDatagramDirect(JNIEnv* env,
 static jint osNetworkSystem_sendConnectedDatagram(JNIEnv* env, jobject,
         jobject fd, jbyteArray data, jint offset, jint length) {
     ScopedByteArrayRO bytes(env, data);
+    if (bytes.get() == NULL) {
+        return -1;
+    }
     return osNetworkSystem_sendConnectedDatagramDirect(env, NULL, fd,
             reinterpret_cast<uintptr_t>(bytes.get()), offset, length);
 }
index c0a6c31..0739621 100644 (file)
@@ -192,6 +192,9 @@ Java_org_apache_harmony_luni_util_NumberConverter_bigIntDigitGeneratorInstImpl (
   fid = env->GetFieldID(clazz, "uArray", "[I");
   uArrayObject = (jintArray) env->GetObjectField(inst, fid);
   ScopedIntArrayRW uArray(env, uArrayObject);
+  if (uArray.get() == NULL) {
+    return;
+  }
 
   getCount = setCount = 0;
   do
index b3980c7..a6ec125 100644 (file)
@@ -419,6 +419,10 @@ static size_t fillBuffer(ParsingContext* parsingContext, const char* characters,
 
     // Decode UTF-8 characters into our buffer.
     ScopedCharArrayRW nativeBuffer(env, buffer);
+    if (nativeBuffer.get() == NULL) {
+        return -1;
+    }
+
     size_t utf16length;
     strcpylen8to16((char16_t*) nativeBuffer.get(), characters, length, &utf16length);
     return utf16length;
@@ -1004,6 +1008,10 @@ static void append(JNIEnv* env, jobject object, jint pointer,
 static void appendBytes(JNIEnv* env, jobject object, jint pointer,
         jbyteArray xml, jint byteOffset, jint byteCount) {
     ScopedByteArrayRO byteArray(env, xml);
+    if (byteArray.get() == NULL) {
+        return;
+    }
+
     const char* bytes = reinterpret_cast<const char*>(byteArray.get());
     append(env, object, pointer, bytes, byteOffset, byteCount, XML_FALSE);
 }
@@ -1011,6 +1019,10 @@ static void appendBytes(JNIEnv* env, jobject object, jint pointer,
 static void appendCharacters(JNIEnv* env, jobject object, jint pointer,
         jcharArray xml, jint charOffset, jint charCount) {
     ScopedCharArrayRO charArray(env, xml);
+    if (charArray.get() == NULL) {
+        return;
+    }
+
     const char* bytes = reinterpret_cast<const char*>(charArray.get());
     size_t byteOffset = 2 * charOffset;
     size_t byteCount = 2 * charCount;