Handle the failure from GetStringUTFChars()

Change-Id: I9eb0f9f0fd2fa5116e7be80b1fa2ed4a95521b9d
related-to-bug: 4139926
diff --git a/media/jni/android_mtp_MtpDatabase.cpp b/media/jni/android_mtp_MtpDatabase.cpp
index 17d39e3..2f88fd1 100644
--- a/media/jni/android_mtp_MtpDatabase.cpp
+++ b/media/jni/android_mtp_MtpDatabase.cpp
@@ -427,6 +427,9 @@
                 jstring stringValue = (jstring)env->GetObjectArrayElement(stringValuesArray, 0);
                 if (stringValue) {
                     const char* str = env->GetStringUTFChars(stringValue, NULL);
+                    if (str == NULL) {
+                        return MTP_RESPONSE_GENERAL_ERROR;
+                    }
                     packet.putString(str);
                     env->ReleaseStringUTFChars(stringValue, str);
                 } else {
diff --git a/media/jni/android_mtp_MtpDevice.cpp b/media/jni/android_mtp_MtpDevice.cpp
index f5fcb4e..40bbaa3 100644
--- a/media/jni/android_mtp_MtpDevice.cpp
+++ b/media/jni/android_mtp_MtpDevice.cpp
@@ -110,6 +110,10 @@
 #ifdef HAVE_ANDROID_OS
     LOGD("open\n");
     const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
+    if (deviceNameStr == NULL) {
+        return false;
+    }
+
     MtpDevice* device = MtpDevice::open(deviceNameStr, fd);
     env->ReleaseStringUTFChars(deviceName, deviceNameStr);
 
@@ -426,12 +430,16 @@
     MtpDevice* device = get_device_from_object(env, thiz);
     if (device) {
         const char *destPathStr = env->GetStringUTFChars(dest_path, NULL);
+        if (destPathStr == NULL) {
+            return false;
+        }
+
         bool result = device->readObject(object_id, destPathStr, AID_SDCARD_RW, 0664);
         env->ReleaseStringUTFChars(dest_path, destPathStr);
         return result;
     }
 #endif
-    return NULL;
+    return false;
 }
 
 // ----------------------------------------------------------------------------
diff --git a/media/jni/android_mtp_MtpServer.cpp b/media/jni/android_mtp_MtpServer.cpp
index c55189f..84c2c7e 100644
--- a/media/jni/android_mtp_MtpServer.cpp
+++ b/media/jni/android_mtp_MtpServer.cpp
@@ -97,7 +97,7 @@
     void removeStorage(MtpStorageID id) {
         MtpStorage* storage = mServer->getStorage(id);
         if (storage) {
-            for (int i = 0; i < mStorageList.size(); i++) {
+            for (size_t i = 0; i < mStorageList.size(); i++) {
                 if (mStorageList[i] == storage) {
                     mStorageList.removeAt(i);
                     break;
@@ -122,7 +122,7 @@
                     (mUsePtp ? MTP_INTERFACE_MODE_PTP : MTP_INTERFACE_MODE_MTP));
 
             mServer = new MtpServer(mFd, mDatabase, AID_MEDIA_RW, 0664, 0775);
-            for (int i = 0; i < mStorageList.size(); i++) {
+            for (size_t i = 0; i < mStorageList.size(); i++) {
                 mServer->addStorage(mStorageList[i]);
             }
         } else {
@@ -247,13 +247,17 @@
         jlong reserveSpace = env->GetLongField(jstorage, field_MtpStorage_reserveSpace);
 
         const char *pathStr = env->GetStringUTFChars(path, NULL);
-        const char *descriptionStr = env->GetStringUTFChars(description, NULL);
-
-        MtpStorage* storage = new MtpStorage(storageID, pathStr, descriptionStr, reserveSpace);
-        thread->addStorage(storage);
-
-        env->ReleaseStringUTFChars(path, pathStr);
-        env->ReleaseStringUTFChars(description, descriptionStr);
+        if (pathStr != NULL) {
+            const char *descriptionStr = env->GetStringUTFChars(description, NULL);
+            if (descriptionStr != NULL) {
+                MtpStorage* storage = new MtpStorage(storageID, pathStr, descriptionStr, reserveSpace);
+                thread->addStorage(storage);
+                env->ReleaseStringUTFChars(path, pathStr);
+                env->ReleaseStringUTFChars(description, descriptionStr);
+            } else {
+                env->ReleaseStringUTFChars(path, pathStr);
+            }
+        }
     } else {
         LOGE("MtpThread is null in add_storage");
     }