From 7ccd9db9aae8fb16fbd157e7ad9a7882d5d51bdc Mon Sep 17 00:00:00 2001 From: Sebastian Rasmussen Date: Tue, 18 Apr 2017 03:52:39 +0800 Subject: jni: Clear java exception when rethrow via fz_throw(). Previously when a java exception was pending CallObjectMethod() was called. This is not permitted and so the exception must be cleared before this function is called. Secondly if the method called by CallObjectMethod() throws an exception this exception was left pending when fz_throw() for unknown java errors was called. Later on jni_rethrow() would be called which in turn called ThrowNew(). This is not permitted either, so the pending exception must be cleared if caused by the call to CallObjectMethod(). Because fz_throw_java() always ends with a fz_throw() it is safe to assume that the pre-existing exception (or the one caused by CallObjectMethod()) will always be converted into a fz_throw(), which in turn will be converted back to a java exception since all calls to fz_throw_java() are enclosed in fz_try() where fz_catch() ends with calling jni_rethrow(). --- platform/java/mupdf_native.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'platform/java') diff --git a/platform/java/mupdf_native.c b/platform/java/mupdf_native.c index b2ec3eb1..3f211d78 100644 --- a/platform/java/mupdf_native.c +++ b/platform/java/mupdf_native.c @@ -251,8 +251,11 @@ static void fz_throw_java(fz_context *ctx, JNIEnv *env) jthrowable ex = (*env)->ExceptionOccurred(env); if (ex) { + (*env)->ExceptionClear(env); jobject msg = (*env)->CallObjectMethod(env, ex, mid_Object_toString); - if (!(*env)->ExceptionCheck(env) && msg) + if ((*env)->ExceptionCheck(env)) + (*env)->ExceptionClear(env); + else if (msg) { const char *p = (*env)->GetStringUTFChars(env, msg, NULL); if (p) -- cgit v1.2.3