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) { \
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) { \
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)) {
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;
} 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);
}
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]);
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;
}
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;
}
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';
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';
// LOGD("Entering arrayToBignum()");
ScopedByteArrayRO sourceBytes(env, source);
+ if (sourceBytes.get() == NULL) {
+ return NULL;
+ }
return BN_bin2bn((unsigned char*) sourceBytes.get(), sourceBytes.size(), NULL);
}
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");
}
ScopedByteArrayRO bufferBytes(env, buffer);
+ if (bufferBytes.get() == NULL) {
+ return;
+ }
EVP_DigestUpdate(ctx, (unsigned char*) (bufferBytes.get() + offset), length);
throwExceptionIfNecessary(env, "NativeCrypto_EVP_DigestUpdate");
}
ScopedByteArrayRO bufferBytes(env, buffer);
+ if (bufferBytes.get() == NULL) {
+ return;
+ }
EVP_VerifyUpdate(ctx, (unsigned char*) (bufferBytes.get() + offset), length);
throwExceptionIfNecessary(env, "NativeCrypto_EVP_VerifyUpdate");
}
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");
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) {
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());
}
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) {
}
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()));
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);
}
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;;
}
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,
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);
}
*/
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);
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());
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)?)
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);
}
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".
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;
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;
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);
}
// 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;
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) {
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);
}
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);
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);
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);
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;
}
ScopedByteArrayRW contextBytes(env, passContext);
+ if (contextBytes.get() == NULL) {
+ return JNI_FALSE;
+ }
selectFDSet* context = reinterpret_cast<selectFDSet*>(contextBytes.get());
int result = 0;
switch (step) {
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);
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);
}
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
// 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;
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);
}
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;