diff src/name/blackcap/exifwasher/exiv2/native.cpp @ 2:efd9fe2d70d7

Rationalize exceptions, code whitelist, add command-line tool.
author David Barts <n5jrn@me.com>
date Wed, 01 Apr 2020 14:23:54 -0700
parents 42277ce58ace
children a59d84674fb0
line wrap: on
line diff
--- a/src/name/blackcap/exifwasher/exiv2/native.cpp	Tue Mar 31 15:38:25 2020 -0700
+++ b/src/name/blackcap/exifwasher/exiv2/native.cpp	Wed Apr 01 14:23:54 2020 -0700
@@ -1,5 +1,6 @@
 #include <jni.h>
 #include <exiv2/exiv2.hpp>
+#include <exception>
 #include <iostream>
 #include <iomanip>
 #include <cassert>
@@ -30,15 +31,10 @@
       jlong ret = 0;
       try {
           ret = reinterpret_cast<jlong> (Exiv2::ImageFactory::open(cPath).release());
-      } catch (...) {
+      } catch (std::exception& e) {
           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);
-          strcpy(message, pfx);
-          strcat(message, cPath);
-          jEnv->ThrowNew(ex, message);
-          free(message);
+          jEnv->ThrowNew(ex, e.what());
       }
       jEnv->ReleaseStringUTFChars(path, cPath);
       return ret;
@@ -55,10 +51,10 @@
       if (jEnv->ExceptionCheck()) return;
       try {
           image->writeMetadata();
-      } catch (...) {
+      } catch (std::exception& e) {
           jEnv->ExceptionClear();
           jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
-          jEnv->ThrowNew(ex, "unable to write metadata");
+          jEnv->ThrowNew(ex, e.what());
       }
   }
 
@@ -73,10 +69,10 @@
       if (jEnv->ExceptionCheck()) return 0;
       try {
           image->readMetadata();
-      } catch (...) {
+      } catch (std::exception& e) {
           jEnv->ExceptionClear();
           jclass ex = jEnv->FindClass("name/blackcap/exifwasher/exiv2/Exiv2Exception");
-          jEnv->ThrowNew(ex, "unable to read metadata");
+          jEnv->ThrowNew(ex, e.what());
           return 0;
       }
       return reinterpret_cast<jlong> (&(image->exifData()));
@@ -118,15 +114,10 @@
       Exiv2::ExifData::iterator found = data->findKey(Exiv2::ExifKey(std::string(cKey)));
       try {
           data->erase(found);
-      } catch (...) {
+      } catch (std::exception& e) {
           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);
-          strcpy(message, pfx);
-          strcat(message, cKey);
-          jEnv->ThrowNew(ex, message);
-          free(message);
+          jEnv->ThrowNew(ex, e.what());
       }
       jEnv->ReleaseStringUTFChars(key, cKey);
   }