Fix static analysis issue 24/325524/1
authorIlho Kim <ilho159.kim@samsung.com>
Wed, 11 Jun 2025 09:16:18 +0000 (18:16 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Wed, 11 Jun 2025 09:16:18 +0000 (18:16 +0900)
Change-Id: I9d85808243a1f30060dc43f6d15eeea8ea1cb50e
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/common/pkgmgr_info_handle.cc
src/common/pkgmgr_info_handle.hh
src/common/shared_memory/shm_app_reader.cc
src/common/shared_memory/shm_pkg_reader.cc
src/pkgmgrinfo_appinfo.cc

index 27c96788074604c885c272f85e563bfd904ade74..3d358a106b8e4ebeef8d23039c41c4a0fab447ed 100644 (file)
@@ -320,14 +320,20 @@ bool PkgInfoHandle::operator<(const PkgInfoHandle& h) {
   return strcmp(GetPackage(), h.GetPackage()) < 0;
 }
 
-PkgInfoHandle PkgInfoHandle::Clone() const {
+std::optional<PkgInfoHandle> PkgInfoHandle::Clone() const {
   uint8_t* raw = (uint8_t*)malloc(data_size_);
+  if (!raw)
+    return std::nullopt;
+
   memcpy(raw, data_, data_size_);
   return PkgInfoHandle(raw, data_size_);
 }
 
-PkgInfoHandle PkgInfoHandle::FromView(const PkgInfoHandleView& view) {
+std::optional<PkgInfoHandle> PkgInfoHandle::FromView(const PkgInfoHandleView& view) {
   uint8_t* raw = (uint8_t*)malloc(view.GetDataSize());
+  if (!raw)
+    return std::nullopt;
+
   memcpy(raw, view.GetData(), view.GetDataSize());
   return PkgInfoHandle(raw, view.GetDataSize());
 }
@@ -454,14 +460,20 @@ bool AppInfoHandle::operator<(const AppInfoHandle& h) {
   return strcmp(GetAppId(), h.GetAppId()) < 0;
 }
 
-AppInfoHandle AppInfoHandle::Clone() const {
+std::optional<AppInfoHandle> AppInfoHandle::Clone() const {
   uint8_t* raw = (uint8_t*)malloc(data_size_);
+  if (!raw)
+    return std::nullopt;
+
   memcpy(raw, data_, data_size_);
   return AppInfoHandle(raw, data_size_);
 }
 
-AppInfoHandle AppInfoHandle::FromView(const AppInfoHandleView& view) {
+std::optional<AppInfoHandle> AppInfoHandle::FromView(const AppInfoHandleView& view) {
   uint8_t* raw = (uint8_t*)malloc(view.GetDataSize());
+  if (!raw)
+    return std::nullopt;
+
   memcpy(raw, view.GetData(), view.GetDataSize());
   return AppInfoHandle(raw, view.GetDataSize());
 }
index 19dca423eddeb62fd2616a44f0fb2518a22f6f2c..4924201813a9993201c5e7ac1808ea9cca3143ad 100644 (file)
@@ -535,8 +535,8 @@ class EXPORT_API PkgInfoHandle : public PkgInfoHandleView {
   PkgInfoHandle& operator=(PkgInfoHandle&& h) noexcept;
   bool operator<(const PkgInfoHandle& h);
 
-  PkgInfoHandle Clone() const;
-  static PkgInfoHandle FromView(const PkgInfoHandleView& view);
+  std::optional<PkgInfoHandle> Clone() const;
+  static std::optional<PkgInfoHandle> FromView(const PkgInfoHandleView& view);
 
  private:
   std::unique_ptr<uint8_t, decltype(&free)> ptr_;
@@ -700,8 +700,8 @@ class EXPORT_API AppInfoHandle : public AppInfoHandleView {
   AppInfoHandle& operator=(AppInfoHandle&& h) noexcept;
   bool operator<(const AppInfoHandle& h);
 
-  AppInfoHandle Clone() const;
-  static AppInfoHandle FromView(const AppInfoHandleView& view);
+  std::optional<AppInfoHandle> Clone() const;
+  static std::optional<AppInfoHandle> FromView(const AppInfoHandleView& view);
 
  private:
   std::unique_ptr<uint8_t, decltype(&free)> ptr_;
index d99a2fba22d7d2fd13f993cb973f11eca837aee1..900d15ec67eebbda798a6a0865c4c48a49d4682e 100644 (file)
@@ -107,8 +107,14 @@ int ShmAppReader::GetHandles(pkgmgrinfo_filter_x* filter,
         else if (result != ShmError::NONE)
           return PMINFO_R_ERROR;
       } else {
-        if (filter_checker.Check(*handle))
-          list.emplace_back(AppInfoHandle::FromView(*handle));
+        if (filter_checker.Check(*handle)) {
+          auto own_handle = AppInfoHandle::FromView(*handle);
+          if (!own_handle) {
+            LOGE("out of memory");
+            return PMINFO_R_ERROR;
+          }
+          list.emplace_back(std::move(*own_handle));
+        }
         break;
       }
     }
@@ -130,8 +136,14 @@ int ShmAppReader::GetHandles(pkgmgrinfo_filter_x* filter,
           continue;
 
         for (const auto& handle : handles->GetHandles())
-          if (filter_checker.Check(handle))
-            list.emplace_back(AppInfoHandle::FromView(handle));
+          if (filter_checker.Check(handle)) {
+            auto own_handle = AppInfoHandle::FromView(handle);
+            if (!own_handle) {
+              LOGE("out of memory");
+              return PMINFO_R_ERROR;
+            }
+            list.emplace_back(std::move(*own_handle));
+          }
       }
     } else {
       std::vector<ShmReader<PkgInfoHandleView>*> pkg_readers;
@@ -175,8 +187,14 @@ int ShmAppReader::GetHandles(pkgmgrinfo_filter_x* filter,
             if (!p_handle)
               continue;
 
-            if (filter_checker.CheckPrivilege(*p_handle))
-              list.emplace_back(AppInfoHandle::FromView(a_handle));
+            if (filter_checker.CheckPrivilege(*p_handle)) {
+              auto own_handle = AppInfoHandle::FromView(a_handle);
+              if (!own_handle) {
+                LOGE("out of memory");
+                return PMINFO_R_ERROR;
+              }
+              list.emplace_back(std::move(*own_handle));
+            }
           }
         }
 
index a6f2b2364d63bca199cb000aa13499abab5d1923..4c5389be5a578d5cc103fd5f316631ed500872e4 100644 (file)
@@ -118,8 +118,14 @@ int ShmPkgReader::GetHandles(pkgmgrinfo_filter_x* filter,
         else if (result != ShmError::NONE)
           return PMINFO_R_ERROR;
       } else {
-        if (filter_checker.Check(*handle))
-          list.emplace_back(PkgInfoHandle::FromView(*handle));
+        if (filter_checker.Check(*handle)) {
+          auto own_handle = PkgInfoHandle::FromView(*handle);
+          if (!own_handle) {
+            LOGE("out of memory");
+            return PMINFO_R_ERROR;
+          }
+          list.emplace_back(std::move(*own_handle));
+        }
         break;
       }
     }
@@ -133,9 +139,16 @@ int ShmPkgReader::GetHandles(pkgmgrinfo_filter_x* filter,
       if (!handles)
         continue;
 
-      for (const auto& handle : handles->GetHandles())
-        if (filter_checker.Check(handle))
-          list.emplace_back(PkgInfoHandle::FromView(handle));
+      for (const auto& handle : handles->GetHandles()) {
+        if (filter_checker.Check(handle)) {
+          auto own_handle = PkgInfoHandle::FromView(handle);
+          if (!own_handle) {
+            LOGE("out of memory");
+            return PMINFO_R_ERROR;
+          }
+          list.emplace_back(std::move(*own_handle));
+        }
+      }
     }
   }
 
index 0d7ea00a668f8e3da850a506b0821d17e1b72ea4..b6476f93c9bc0a10a4dbad9650ea248c676deb40 100644 (file)
@@ -300,6 +300,12 @@ API int pkgmgrinfo_appinfo_clone_appinfo(pkgmgrinfo_appinfo_h handle,
   if (handle == NULL)
     return PMINFO_R_EINVAL;
 
+  auto clone_handle = temp->app_info->Clone();
+  if (!clone_handle) {
+    LOGE("memory alloc failed");
+    return PMINFO_R_ERROR;
+  }
+
   info = reinterpret_cast<pkgmgr_appinfo_x*>(
       calloc(1, sizeof(pkgmgr_appinfo_x)));
   if (info == NULL) {
@@ -308,7 +314,7 @@ API int pkgmgrinfo_appinfo_clone_appinfo(pkgmgrinfo_appinfo_h handle,
   }
 
   info->app_component = temp->app_component;
-  info->app_info = new pc::AppInfoHandle(temp->app_info->Clone());
+  info->app_info = new pc::AppInfoHandle(std::move(*clone_handle));
   info->locale = info->app_info->GetLocale();
   info->package = info->app_info->GetPackage();