From ca3be996bca9e3a8ef8feb0f6776f08f52f6e53e Mon Sep 17 00:00:00 2001 From: olly Date: Thu, 1 Feb 2018 04:19:02 -0800 Subject: [PATCH] Add proper exception checks when returning to native from Java. The pending exception will be thrown upon returning to Java from native, but we should return early rather than continuing to execute the native method to the end so as to avoid undefined behavior. Note that the return value is irrelevant (because the pending exception will be thrown). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=184119606 --- extensions/flac/src/main/jni/flac_jni.cc | 3 ++- extensions/opus/src/main/jni/opus_jni.cc | 8 ++++++++ extensions/vp9/src/main/jni/vpx_jni.cc | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/extensions/flac/src/main/jni/flac_jni.cc b/extensions/flac/src/main/jni/flac_jni.cc index c9e5d7ab36..59f37b0c2e 100644 --- a/extensions/flac/src/main/jni/flac_jni.cc +++ b/extensions/flac/src/main/jni/flac_jni.cc @@ -50,7 +50,8 @@ class JavaDataSource : public DataSource { ssize_t readAt(off64_t offset, void *const data, size_t size) { jobject byteBuffer = env->NewDirectByteBuffer(data, size); int result = env->CallIntMethod(flacDecoderJni, mid, byteBuffer); - if (env->ExceptionOccurred()) { + if (env->ExceptionCheck()) { + // Exception is thrown in Java when returning from the native call. result = -1; } env->DeleteLocalRef(byteBuffer); diff --git a/extensions/opus/src/main/jni/opus_jni.cc b/extensions/opus/src/main/jni/opus_jni.cc index 8d9c1a4152..9042e4cb89 100644 --- a/extensions/opus/src/main/jni/opus_jni.cc +++ b/extensions/opus/src/main/jni/opus_jni.cc @@ -103,8 +103,16 @@ DECODER_FUNC(jint, opusDecode, jlong jDecoder, jlong jTimeUs, kMaxOpusOutputPacketSizeSamples * kBytesPerSample * channelCount; env->CallObjectMethod(jOutputBuffer, outputBufferInit, jTimeUs, outputSize); + if (env->ExceptionCheck()) { + // Exception is thrown in Java when returning from the native call. + return -1; + } const jobject jOutputBufferData = env->CallObjectMethod(jOutputBuffer, outputBufferInit, jTimeUs, outputSize); + if (env->ExceptionCheck()) { + // Exception is thrown in Java when returning from the native call. + return -1; + } int16_t* outputBufferData = reinterpret_cast( env->GetDirectBufferAddress(jOutputBufferData)); diff --git a/extensions/vp9/src/main/jni/vpx_jni.cc b/extensions/vp9/src/main/jni/vpx_jni.cc index 1f36250e10..421b16d26d 100644 --- a/extensions/vp9/src/main/jni/vpx_jni.cc +++ b/extensions/vp9/src/main/jni/vpx_jni.cc @@ -362,7 +362,7 @@ DECODER_FUNC(jint, vpxGetFrame, jlong jContext, jobject jOutputBuffer) { // resize buffer if required. jboolean initResult = env->CallBooleanMethod(jOutputBuffer, initForRgbFrame, img->d_w, img->d_h); - if (initResult == JNI_FALSE) { + if (env->ExceptionCheck() || !initResult) { return -1; } @@ -400,7 +400,7 @@ DECODER_FUNC(jint, vpxGetFrame, jlong jContext, jobject jOutputBuffer) { jboolean initResult = env->CallBooleanMethod( jOutputBuffer, initForYuvFrame, img->d_w, img->d_h, img->stride[VPX_PLANE_Y], img->stride[VPX_PLANE_U], colorspace); - if (initResult == JNI_FALSE) { + if (env->ExceptionCheck() || !initResult) { return -1; } -- GitLab