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());
}
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());
}
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_;
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_;
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;
}
}
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;
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));
+ }
}
}
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;
}
}
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));
+ }
+ }
}
}
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) {
}
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();