Fix memory leak 82/213082/2
authorSangyoon Jang <jeremy.jang@samsung.com>
Fri, 30 Aug 2019 05:52:58 +0000 (14:52 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Mon, 2 Sep 2019 01:41:56 +0000 (10:41 +0900)
Change-Id: Iab2eebe7a83897c5be850a7aecaf59327018c31a
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/common/pkgmgr_registration.cc
src/common/step/pkgmgr/step_check_force_clean.cc
src/common/utils/manifest_util.cc
src/common/utils/manifest_util.h

index fc67aa0..92662e8 100644 (file)
@@ -171,13 +171,11 @@ bool UpgradeAppInPkgmgr(manifest_x* manifest,
 
 bool UnregisterAppInPkgmgrForPkgId(const std::string& pkgid, uid_t uid,
                                    RequestMode request_mode) {
-  manifest_x* manifest =
-     static_cast<manifest_x*>(calloc(1, sizeof(manifest_x)));
+  manifest_x* manifest = PkgmgrGenerateManifestInfoFromDB(pkgid, uid);
   if (!manifest) {
-    LOG(ERROR) << "Out of memory";
+    LOG(ERROR) << "Failed to get manifest_x from DB";
     return false;
   }
-  PkgmgrGenerateManifestInfoFromDB(manifest, pkgid, uid);
   bool res = UnregisterAppInPkgmgr(manifest, pkgid, uid, request_mode);
   pkgmgr_parser_free_manifest_xml(manifest);
   return res;
index bc29170..95cc203 100644 (file)
@@ -35,16 +35,14 @@ Step::Status StepCheckForceClean::process() {
   if (!bf::exists(xml_path) &&
       pkg_query.IsPackageInstalled()) {
     context_->force_clean_from_db.set(true);
-    manifest_x* manifest =
-        static_cast<manifest_x*>(calloc(1, sizeof(manifest_x)));
+    manifest_x* manifest = PkgmgrGenerateManifestInfoFromDB(
+        context_->pkgid.get(),context_->uid.get());
     if (!manifest) {
-      LOG(ERROR) << "Out of memory";
+      LOG(ERROR) << "Failed to get manifest_x from DB";
       return Status::ERROR;
     }
-    if (!PkgmgrGenerateManifestInfoFromDB(manifest, context_->pkgid.get(),
-            context_->uid.get())) {
-      context_->manifest_data.set(manifest);
-    }
+
+    context_->manifest_data.set(manifest);
   }
 
   return Status::OK;
index 5d22d53..2188880 100644 (file)
@@ -76,31 +76,36 @@ bf::path GetManifestLocation(const std::string& pkgid,
   return xml_path;
 }
 
-int PkgmgrGenerateManifestInfoFromDB(manifest_x* mfx,
-                                     const std::string &pkgid,
-                                     uid_t uid) {
+manifest_x* PkgmgrGenerateManifestInfoFromDB(const std::string &pkgid,
+                                             uid_t uid) {
   pkgmgrinfo_appinfo_filter_h filter;
-  mfx->package = strdup(pkgid.c_str());
-
   if (pkgmgrinfo_appinfo_filter_create(&filter))
-    return PMINFO_R_ERROR;
+    return nullptr;
 
   if (pkgmgrinfo_appinfo_filter_add_string(filter,
                           PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid.c_str())) {
     pkgmgrinfo_appinfo_filter_destroy(filter);
-    return PMINFO_R_ERROR;
+    return nullptr;
   }
 
+  manifest_x* mfx = static_cast<manifest_x*>(calloc(1, sizeof(manifest_x)));
+  if (!mfx) {
+    pkgmgrinfo_appinfo_filter_destroy(filter);
+    LOG(ERROR) << "Out of memory";
+    return nullptr;
+  }
   if (pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(filter,
                                                     PkgmgrAppInfoCallback,
                                                     mfx, uid)) {
+    pkgmgr_parser_free_manifest_xml(mfx);
     pkgmgrinfo_appinfo_filter_destroy(filter);
-    return PMINFO_R_ERROR;
+    return nullptr;
   }
+  mfx->package = strdup(pkgid.c_str());
 
   pkgmgrinfo_appinfo_filter_destroy(filter);
 
-  return PMINFO_R_OK;
+  return mfx;
 }
 
 }  // namespace common_installer
index fd22412..e2f07ea 100644 (file)
@@ -14,9 +14,8 @@
 namespace bf = boost::filesystem;
 namespace common_installer {
 
-int PkgmgrGenerateManifestInfoFromDB(manifest_x* mfx,
-                                     const std::string& pkgid,
-                                     uid_t uid);
+manifest_x* PkgmgrGenerateManifestInfoFromDB(const std::string& pkgid,
+                                             uid_t uid);
 
 bf::path GetManifestLocation(const std::string& pkgid,
                              uid_t uid,