diff src/name/blackcap/exifwasher/exiv2/native.cpp @ 1:42277ce58ace

Improve exception handling; always use absolute paths on libs.
author David Barts <n5jrn@me.com>
date Tue, 31 Mar 2020 15:38:25 -0700
parents db63d01a23c6
children efd9fe2d70d7
line wrap: on
line diff
--- a/src/name/blackcap/exifwasher/exiv2/native.cpp	Tue Mar 31 13:24:48 2020 -0700
+++ b/src/name/blackcap/exifwasher/exiv2/native.cpp	Tue Mar 31 15:38:25 2020 -0700
@@ -31,6 +31,7 @@
       try {
           ret = reinterpret_cast<jlong> (Exiv2::ImageFactory::open(cPath).release());
       } catch (...) {
+          jEnv->ExceptionClear();
           jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
           const char *pfx = "unable to open ";
           char *message = (char *) malloc(strlen(cPath) + strlen(pfx) + 1);
@@ -51,9 +52,11 @@
 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1writeMetadata
   (JNIEnv *jEnv, jobject jThis) {
       Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
+      if (jEnv->ExceptionCheck()) return;
       try {
           image->writeMetadata();
       } catch (...) {
+          jEnv->ExceptionClear();
           jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
           jEnv->ThrowNew(ex, "unable to write metadata");
       }
@@ -67,11 +70,14 @@
 JNIEXPORT jlong JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1getMetadata
   (JNIEnv *jEnv, jobject jThis) {
       Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
+      if (jEnv->ExceptionCheck()) return 0;
       try {
           image->readMetadata();
       } catch (...) {
+          jEnv->ExceptionClear();
           jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
           jEnv->ThrowNew(ex, "unable to read metadata");
+          return 0;
       }
       return reinterpret_cast<jlong> (&(image->exifData()));
   }
@@ -84,6 +90,7 @@
 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_Image__1dtor
   (JNIEnv *jEnv, jobject jThis) {
       Exiv2::Image *image = reinterpret_cast<Exiv2::Image *> (getPointer(jEnv, jThis));
+      if (jEnv->ExceptionCheck()) return;
       delete image;
   }
 
@@ -106,11 +113,13 @@
 JNIEXPORT void JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1erase
   (JNIEnv *jEnv, jobject jThis, jstring key) {
       Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis));
+      if (jEnv->ExceptionCheck()) return;
       const char *cKey = jEnv->GetStringUTFChars(key, NULL);
       Exiv2::ExifData::iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey)));
       try {
           data->erase(found);
       } catch (...) {
+          jEnv->ExceptionClear();
           jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
           const char *pfx = "unable to delete ";
           char *message = (char *) malloc(strlen(cKey) + strlen(pfx) + 1);
@@ -130,6 +139,7 @@
 JNIEXPORT jobject JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1value
   (JNIEnv *jEnv, jobject jThis, jstring key) {
       Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis));
+      if (jEnv->ExceptionCheck()) return NULL;
       const char *cKey = jEnv->GetStringUTFChars(key, NULL);
       Exiv2::ExifData::const_iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey)));
       jEnv->ReleaseStringUTFChars(key, cKey);
@@ -137,9 +147,11 @@
           return NULL;
       }
       jclass klass = jEnv->FindClass("name/blackcap/exifwasher/exiv2/ExifData$Value");
+      if (jEnv->ExceptionCheck()) return NULL;
       jstring type = jEnv->NewStringUTF(found->typeName());
       jstring value = jEnv->NewStringUTF(found->toString().c_str());
       jmethodID method = jEnv->GetMethodID(klass, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
+      if (jEnv->ExceptionCheck()) return NULL;
       return jEnv->NewObject(klass, method, type, value);
   }
 
@@ -151,12 +163,16 @@
 JNIEXPORT jobjectArray JNICALL Java_name_blackcap_exifwasher_exiv2_ExifData__1keys
   (JNIEnv *jEnv, jobject jThis) {
       Exiv2::ExifData *data = reinterpret_cast<Exiv2::ExifData *> (getPointer(jEnv, jThis));
+      if (jEnv->ExceptionCheck()) return NULL;
       jclass klass = jEnv->FindClass("java/lang/String");
+      if (jEnv->ExceptionCheck()) return NULL;
       jobjectArray ret = jEnv->NewObjectArray(data->count(), klass, NULL);
+      if (jEnv->ExceptionCheck()) return NULL;
       Exiv2::ExifData::const_iterator end = data->end();
       jsize j = 0;
       for (Exiv2::ExifData::const_iterator i = data->begin(); i != end; ++i) {
           jEnv->SetObjectArrayElement(ret, j++, jEnv->NewStringUTF(i->key().c_str()));
+          if (jEnv->ExceptionCheck()) break;
       }
       return ret;
   }