Add exception handling codes
authorJusung Son <jusung07.son@samsung.com>
Fri, 12 Jul 2019 00:59:38 +0000 (09:59 +0900)
committerJusung Son <jusung07.son@samsung.com>
Fri, 12 Jul 2019 07:00:30 +0000 (16:00 +0900)
Change-Id: Id21db779d4b121206299d69b38756245f5e12c05
Signed-off-by: Jusung Son <jusung07.son@samsung.com>
notification-ex/stub.cc

index 341c2e87ea46cb9ceb1ae4a4747234a997a1f4da..6eb09339314632be519e5f71fe504f88995b1591 100644 (file)
@@ -932,10 +932,22 @@ extern "C" EXPORT_API int noti_ex_item_input_selector_get_contents(
   InputSelectorItem* p = static_cast<InputSelectorItem*>(h->Get());
   list<string> contents = p->GetContents();
   char **list = (char**)calloc(contents.size(), sizeof(char*));
+  if (list == nullptr) {
+    LOGE("Out of memory");
+    return NOTI_EX_ERROR_OUT_OF_MEMORY;
+  }
+
   int idx = 0;
   for (auto& i : contents) {
-    list[idx++] = strdup(i.c_str());
+    list[idx] = strdup(i.c_str());
+    if (list[idx] == nullptr) {
+      __noti_ex_free_str_array(list, idx);
+      LOGE("Out of memory");
+      return NOTI_EX_ERROR_OUT_OF_MEMORY;
+    }
+    idx++;
   }
+
   *count = contents.size();
   *contents_list = list;
 
@@ -1576,11 +1588,24 @@ extern "C" EXPORT_API int noti_ex_item_get_shared_paths(noti_ex_item_h handle,
   }
   Handle* p = static_cast<Handle*>(handle);
   list<string> shared_path = p->Get()->GetSharedPath();
-  *path = (char**)calloc(shared_path.size(), sizeof(char*));
+  char** tmp_path = (char**)calloc(shared_path.size(), sizeof(char*));
+  if (tmp_path == nullptr) {
+    LOGE("Fail to create items");
+    return NOTI_EX_ERROR_OUT_OF_MEMORY;
+  }
+
   int idx = 0;
   for (auto& i : shared_path) {
-    *path[idx++] = strdup(i.c_str());
+    tmp_path[idx] = strdup(i.c_str());
+    if (tmp_path[idx] == nullptr) {
+      __noti_ex_free_str_array(tmp_path, idx);
+      LOGE("Out of memory");
+      return NOTI_EX_ERROR_OUT_OF_MEMORY;
+    }
+    idx++;
   }
+
+  *path = tmp_path;
   *count = shared_path.size();
   return NOTI_EX_ERROR_NONE;
 }
@@ -1744,11 +1769,24 @@ extern "C" EXPORT_API int noti_ex_item_get_receiver_list(noti_ex_item_h handle,
 
   Handle* p = static_cast<Handle*>(handle);
   list<string> receivers = p->Get()->GetReceiverList();
-  *receiver_list = (char**)calloc(receivers.size(), sizeof(char*));
+  char **tmp_list = (char**)calloc(receivers.size(), sizeof(char*));
+  if (tmp_list == nullptr) {
+    LOGE("Out of memory");
+    return NOTI_EX_ERROR_OUT_OF_MEMORY;
+  }
+
   int idx = 0;
   for (auto& i : receivers) {
-    *receiver_list[idx++] = strdup(i.c_str());
+    tmp_list[idx] = strdup(i.c_str());
+    if (tmp_list[idx] == nullptr) {
+      __noti_ex_free_str_array(tmp_list, idx);
+      LOGE("Out of memory");
+      return NOTI_EX_ERROR_OUT_OF_MEMORY;
+    }
+    idx++;
   }
+
+  *receiver_list = tmp_list;
   *count = receivers.size();
   return NOTI_EX_ERROR_NONE;
 }