From c5de9439054c9bc3112b8f93dfea9e477c5ccdb5 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Mon, 27 Jan 2025 16:14:36 +0530 Subject: [PATCH 1/2] Fixed the native crash happening on Android. * Improved the `getPtr` method to throw a `NativeHandleDisposedException` when a native object is `nullptr`, updated the `CATCH_EXCEPTION` macro to handle this exception by mapping it to `java.lang.IllegalStateException`, and ensured proper error handling to prevent crashes when accessing disposed objects. --- lib/src/main/cpp/macros.h | 3 +++ lib/src/main/cpp/utils.h | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/src/main/cpp/macros.h b/lib/src/main/cpp/macros.h index 46201d5..5a31e51 100644 --- a/lib/src/main/cpp/macros.h +++ b/lib/src/main/cpp/macros.h @@ -53,6 +53,9 @@ catch(const zim::ZimFileFormatError& e) { \ } catch(const zim::EntryNotFound& e) { \ throwException(env, "org/kiwix/libzim/EntryNotFoundException", e.what()); \ return RET; \ +} catch (const NativeHandleDisposedException& e) { \ + throwException(env, "java/lang/IllegalStateException", e.what()); \ + return RET; \ } catch (const std::ios_base::failure& e) { \ throwException(env, "java/io/IOException", e.what()); \ return RET; \ diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index 9324adf..c5d7d12 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -96,6 +96,11 @@ inline void setHandle(JNIEnv* env, jobject thisObj, Args && ...args) } #define SET_HANDLE(NATIVE_TYPE, OBJ, VALUE) setHandle(env, OBJ, VALUE) +class NativeHandleDisposedException : public std::runtime_error { +public: + explicit NativeHandleDisposedException(const std::string& message) + : std::runtime_error(message) {} +}; // Return a shared_ptr for the handle template @@ -104,6 +109,9 @@ shared_ptr getPtr(JNIEnv* env, jobject thisObj, const char* handleName = "nat jclass thisClass = env->GetObjectClass(thisObj); jfieldID fidNumber = env->GetFieldID(thisClass, handleName, "J"); auto handle = reinterpret_cast*>(env->GetLongField(thisObj, fidNumber)); + if (handle == nullptr) { + throw NativeHandleDisposedException("The native object is already has been disposed"); + } return *handle; } #define GET_PTR(NATIVE_TYPE) getPtr(env, thisObj) From d7c38990dfab51837c60e4203549ea8cc8128d8f Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Mon, 27 Jan 2025 16:34:41 +0530 Subject: [PATCH 2/2] Corrected the error message. --- lib/src/main/cpp/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index c5d7d12..a4beb3f 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -110,7 +110,7 @@ shared_ptr getPtr(JNIEnv* env, jobject thisObj, const char* handleName = "nat jfieldID fidNumber = env->GetFieldID(thisClass, handleName, "J"); auto handle = reinterpret_cast*>(env->GetLongField(thisObj, fidNumber)); if (handle == nullptr) { - throw NativeHandleDisposedException("The native object is already has been disposed"); + throw NativeHandleDisposedException("The native object has already been disposed"); } return *handle; }