Fix static analysis issues 76/259576/2
authorIlho Kim <ilho159.kim@samsung.com>
Thu, 10 Jun 2021 01:32:26 +0000 (10:32 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Thu, 10 Jun 2021 06:18:30 +0000 (15:18 +0900)
 - Fix memory leak
 - Add try-catch statement to main
 - Fix Uninitialized scalar field

Change-Id: If270567d9e33f77ca771badbf1de7f32a14d47da
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/common/installer_context.h
src/common/step/filesystem/step_move_installed_storage.cc
src/common/step/filesystem/step_move_installed_storage.h
src/common/utils/user_util.cc
src/pkg_initdb/pkg_initdb.cc
src/pkg_recovery/pkg_recovery.cc

index 99edb45..c7e03bf 100644 (file)
@@ -156,6 +156,7 @@ enum class Storage : int {
  * Enumeration for move types
  */
 enum class MoveType : int {
+  NONE = -1,
   TO_INTERNAL = 0,
   TO_EXTERNAL = 1,
   TO_EXTENDED = 2
index 62e3dd0..f61b1dd 100644 (file)
@@ -50,6 +50,10 @@ bool MoveFileAndUpdateTepInfo(const boost::filesystem::path& src,
 namespace common_installer {
 namespace filesystem {
 
+StepMoveInstalledStorage::StepMoveInstalledStorage(InstallerContext* context)
+    : Step(context), move_type_(MoveType::NONE) {
+}
+
 Step::Status StepMoveInstalledStorage::process() {
   PkgQueryInterface pkg_query(context_->pkgid.get(),
       context_->uid.get());
index 51ec2f5..89d1844 100644 (file)
@@ -15,6 +15,8 @@ class StepMoveInstalledStorage : public Step {
  public:
   using Step::Step;
 
+  explicit StepMoveInstalledStorage(InstallerContext* context);
+
   Status process() override;
 
   Status clean() override;
index da41147..6f01bbc 100644 (file)
@@ -71,8 +71,10 @@ UserList GetUserList() {
       break;
     }
   }
-  if (gum_user_list == nullptr)
+  if (gum_user_list == nullptr) {
+    g_strfreev(user_type_strv);
     return {};
+  }
 
   UserList list;
   for (GumUser* guser : GListRange<GumUser*>(gum_user_list)) {
index a7b7ab5..b3f881b 100644 (file)
@@ -23,19 +23,24 @@ void AddErrorFlag() {
 }  // namespace
 
 int main(int argc, char *argv[]) {
-  if (getuid() != kRootUserUid) {
-    std::cerr << "This binary should be run as root user" << std::endl;
+  try {
+    if (getuid() != kRootUserUid) {
+      std::cerr << "This binary should be run as root user" << std::endl;
+      return -1;
+    }
+
+    InitPkgDB init_pkg_db;
+    if (!init_pkg_db.Init(argc, argv))
+      return -1;
+
+    if (!init_pkg_db.Run()) {
+      AddErrorFlag();
+      return -1;
+    }
+
+    return 0;
+  } catch(...) {
+    std::cerr << "Exception occured" << std::endl;
     return -1;
   }
-
-  InitPkgDB init_pkg_db;
-  if (!init_pkg_db.Init(argc, argv))
-    return -1;
-
-  if (!init_pkg_db.Run()) {
-    AddErrorFlag();
-    return -1;
-  }
-
-  return 0;
 }
index 0ba257e..8816257 100644 (file)
@@ -196,7 +196,12 @@ void PkgRecoveryService::ProcessRecovery(uid_t uid,
 }  // namespace
 
 int main() {
-  PkgRecoveryService service;
-  service.Run();
-  return 0;
+  try {
+    PkgRecoveryService service;
+    service.Run();
+    return 0;
+  } catch(...) {
+    LOG(ERROR) << "Exception occured";
+    return -1;
+  }
 }