Fix CategoryPlugin, MetadataPlugin
[platform/core/appfw/app-installers.git] / src / common / plugins / metadata_plugin.cc
index c11a577..cf2952a 100644 (file)
@@ -30,37 +30,39 @@ void ClearMetadataDetail(gpointer data) {
 }
 
 GList* GetMetadataListForKey(GList* list, const std::string& key) {
-  // pack all metadata starting with key to list that will
-  // be sent to the plugin.
-  // e.g. all http://developer.samsung.com/tizen/metadata/profile/*
-  //   will be packed for http://developer.samsung.com/tizen/metadata/profile
   GList* md_list = nullptr;
   for (metadata_x* meta : GListRange<metadata_x*>(list)) {
-    if (meta->key && meta->value &&
-        std::string(meta->key).find(key) == 0) {
-      __metadata_t* md = reinterpret_cast<__metadata_t*>(
-          calloc(1, sizeof(__metadata_t)));
-      if (!md) {
-        LOG(ERROR) << "Out of memory";
-        g_list_free_full(md_list, &ClearMetadataDetail);
-        return nullptr;
-      }
-      md->key = strdup(meta->key);
-      if (!md->key) {
-        LOG(ERROR) << "Out of memory";
-        free(md);
-        g_list_free_full(md_list, &ClearMetadataDetail);
-        return nullptr;
-      }
-      md->value = strdup(meta->value);
-      if (!md->value) {
-        LOG(ERROR) << "Out of memory";
-        ClearMetadataDetail(md);
-        g_list_free_full(md_list, &ClearMetadataDetail);
-        return nullptr;
-      }
-      md_list = g_list_append(md_list, md);
+    // key and val should not be null (at least empty string)
+    if (!meta->key || !meta->val) {
+      LOG(ERROR) << "Metadata key or val is null";
+      continue;
+    }
+
+    if (std::string(meta->key).find(key) != 0)
+      continue;
+
+    __metadata_t* md = reinterpret_cast<__metadata_t*>(
+        calloc(1, sizeof(__metadata_t)));
+    if (!md) {
+      LOG(ERROR) << "Out of memory";
+      g_list_free_full(md_list, &ClearMetadataDetail);
+      return nullptr;
+    }
+    md->key = strdup(meta->key);
+    if (!md->key) {
+      LOG(ERROR) << "Out of memory";
+      free(md);
+      g_list_free_full(md_list, &ClearMetadataDetail);
+      return nullptr;
     }
+    md->value = strdup(meta->value);
+    if (!md->value) {
+      LOG(ERROR) << "Out of memory";
+      ClearMetadataDetail(md);
+      g_list_free_full(md_list, &ClearMetadataDetail);
+      return nullptr;
+    }
+    md_list = g_list_append(md_list, md);
   }
   return md_list;
 }
@@ -169,6 +171,10 @@ bool MetadataPlugin::Run(xmlDocPtr /*doc_ptr*/, manifest_x* manifest,
   }
 
   bool result;
+  // pack all metadata starting with key to list that will
+  // be sent to the plugin.
+  // e.g. all http://developer.samsung.com/tizen/metadata/profile/*
+  //   will be packed for http://developer.samsung.com/tizen/metadata/profile
   GList* md_list = GetMetadataListForKey(manifest->metadata,
       plugin_info_.name());
   if (md_list) {