Fix ResultParcelable to handle null char pointer
[platform/core/appfw/pkgmgr-info.git] / src / common / request_handler / set_pkginfo_request_handler.cc
1 // Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "set_pkginfo_request_handler.hh"
6
7 #include <string>
8
9 #include "parcelable_factory.hh"
10 #include "pkginfo_parcelable.hh"
11 #include "pkg_set_db_handler.hh"
12
13 #include "pkgmgr_parser.h"
14
15 #include "pkgmgrinfo_debug.h"
16
17 namespace pcp = pkgmgr_common::parcel;
18 namespace pcd = pkgmgr_common::database;
19
20 namespace pkgmgr_server {
21 namespace request_handler {
22
23 bool SetPkginfoRequestHandler::HandleRequest(unsigned char* data, int size,
24                                              const std::string& locale) {
25   auto abstract_parcel =
26       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
27
28   if (abstract_parcel == nullptr ||
29       abstract_parcel->GetType() != pcp::ParcelableType::PkgInfo) {
30     _LOGE("Invalid parcel or type");
31     result_ = std::make_shared<pcp::ResultParcelable>(
32         PM_PARSER_R_ERROR, std::vector<pcp::StrArgs>{});
33     return false;
34   }
35
36   auto* parcel = dynamic_cast<pcp::PkgInfoParcelable*>(abstract_parcel.get());
37   if (parcel == nullptr) {
38     _LOGE("Parcel is empty");
39     result_ = std::make_shared<pcp::ResultParcelable>(
40         PM_PARSER_R_ERROR, std::vector<pcp::StrArgs>{});
41     return false;
42   }
43
44   pcd::PkgSetDBHandler db(parcel->GetUid(), GetPID());
45   db.SetLocale(locale);
46   db.SetWriteType(parcel->GetWriteType());
47
48   int ret = PM_PARSER_R_ERROR;
49
50   for (auto& i : parcel->GetPkgInfo()) {
51     db.SetPkgInfo(i);
52     ret = db.Execute();
53     if (ret != PM_PARSER_R_OK) {
54       _LOGE("Failed to set pkginfo");
55       break;
56     }
57   }
58
59   result_ = std::make_shared<pcp::ResultParcelable>(
60       ret, std::vector<pcp::StrArgs>{});
61
62   return true;
63 }
64
65 std::vector<uint8_t> SetPkginfoRequestHandler::ExtractResult() {
66   tizen_base::Parcel parcel;
67
68   parcel.WriteParcelable(*result_);
69   std::vector<uint8_t> raw = parcel.GetRaw();
70
71   result_.reset();
72
73   return raw;
74 }
75
76 }  // namespace request_handler
77 }  // namespace pkgmgr_server