Fix static analysis issues 09/291609/3
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 19 Apr 2023 06:43:51 +0000 (06:43 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 19 Apr 2023 08:52:05 +0000 (08:52 +0000)
The following cases are fixed:
 - resource leak
 - no effect

Change-Id: I01736acf26cb18d262ccbc0abae6a06ea1e315d5
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
idlc/gen/version2/c_body_generator_base_cb.hh
idlc/gen/version2/c_proxy_body_generator_cb.hh
idlc/gen/version2/c_stub_body_generator.cc
idlc/gen/version2/c_stub_body_generator.hh
idlc/gen/version2/c_stub_body_generator_cb.hh

index 770874c9a8f70a6cd2b1ca880ee524c4c79aa013..e13dfd883fbb63c12fe9a6630feafe2f1ba16f59 100644 (file)
@@ -406,11 +406,6 @@ static int rpc_port_unit_map_read_<TYPE_NAME>(rpc_port_unit_map_h unit_map,
   if (unit_map == nullptr || name == nullptr || value == nullptr)
     return RPC_PORT_ERROR_INVALID_PARAMETER;
 
-  ret = <PARAM_TYPE_CTOR>(&h);
-  if (ret != RPC_PORT_ERROR_NONE)
-    return ret;
-
-  *value = h;
   unit = g_hash_table_lookup(unit_map->table, name);
   if (unit == nullptr) {
     _E("g_hash_table_lookup() is failed. name: %s", name);
@@ -422,6 +417,11 @@ static int rpc_port_unit_map_read_<TYPE_NAME>(rpc_port_unit_map_h unit_map,
     return RPC_PORT_ERROR_INVALID_PARAMETER;
   }
 
+  ret = <PARAM_TYPE_CTOR>(&h);
+  if (ret != RPC_PORT_ERROR_NONE)
+    return ret;
+
+  *value = h;
   rpc_port_parcel_read(unit->parcel, &h->parcelable, h);
 
   return get_last_result();
@@ -439,11 +439,6 @@ static int rpc_port_unit_map_read_delegate(rpc_port_unit_map_h unit_map,
   if (unit_map == nullptr || name == nullptr || value == nullptr)
     return RPC_PORT_ERROR_INVALID_PARAMETER;
 
-  delegate = rpc_port_delegate_create();
-  if (delegate == NULL)
-    return RPC_PORT_ERROR_OUT_OF_MEMORY;
-
-  *value = delegate;
   unit = g_hash_table_lookup(unit_map->table, name);
   if (unit == nullptr) {
     _E("g_hash_table_lookup() is failed. name: %s", name);
@@ -455,6 +450,11 @@ static int rpc_port_unit_map_read_delegate(rpc_port_unit_map_h unit_map,
     return RPC_PORT_ERROR_INVALID_PARAMETER;
   }
 
+  delegate = rpc_port_delegate_create();
+  if (delegate == NULL)
+    return RPC_PORT_ERROR_OUT_OF_MEMORY;
+
+  *value = delegate;
   rpc_port_parcel_read(unit->parcel, &delegate->parcelable, delegate);
 
   return get_last_result();
index d2e1ca47d15104eb8d574004512cd36901e52470..9deb02aa421d776b85dc14c23dbbcc826d422da4 100644 (file)
@@ -716,6 +716,7 @@ static void __<PREFIX>_<NAME>_consume_command(rpc_port_h port, int seq_num, rpc_
     rpc_port_unit_map_clear(map);
   } while (true);
 
+  rpc_port_unit_map_destroy(map);
   *unit_map = nullptr;
 }
 
index 96c2169ee19f74666298417d3dd57ea1499f9304..828f298590d114ad777817bd586320dca020359b 100644 (file)
@@ -279,7 +279,8 @@ void CStubBodyGenerator::GenInterfaces(std::ofstream& stream) {
   }
 }
 
-void CStubBodyGenerator::GenInterface(std::ofstream& stream, const Interface& iface) {
+void CStubBodyGenerator::GenInterface(std::ofstream& stream,
+    const Interface& iface) {
   bool has_delegate = false;
   for (const auto& d : iface.GetDeclarations()) {
     if (d->GetMethodType() != Declaration::MethodType::DELEGATE)
@@ -443,10 +444,11 @@ std::string CStubBodyGenerator::GenMethodUnitMapRead(const Interface& iface,
     } else if (type.GetUserDefinedType() == BaseType::UserType::STRUCTURE ||
         type.GetMetaType() != nullptr ||
         type.GetKeyType() != nullptr) {
-      parcel_read_code = ReplaceAll(CB_INTERFACE_METHOD_USER_DEFINED_UNIT_MAP_READ)
-          .Change("<TYPE_NAME>", GetFullNameFromType(type, iface))
-          .Change("<ARG_NAME>", p->GetID())
-          .Change("<ARG>", p->GetID());
+      parcel_read_code =
+          ReplaceAll(CB_INTERFACE_METHOD_USER_DEFINED_UNIT_MAP_READ)
+              .Change("<TYPE_NAME>", GetFullNameFromType(type, iface))
+              .Change("<ARG_NAME>", p->GetID())
+              .Change("<ARG>", p->GetID());
     } else if (type.ToString() == "bundle") {
       parcel_read_code = ReplaceAll(CB_INTERFACE_METHOD_BUNDLE_UNIT_MAP_READ)
           .Change("<ARG_NAME>", p->GetID())
@@ -518,19 +520,20 @@ std::string CStubBodyGenerator::GenMethodUnitMapWriteBase(
 
   if (type.GetUserDefinedType() == BaseType::UserType::DELEGATE) {
     parcel_write_code = ReplaceAll(CB_INTERFACE_METHOD_DELEGATE_UNIT_MAP_WRITE)
-         .Change("<ARG_NAME>", std::string("delegate"))
-         .Change("<ARG>", arg);
+        .Change("<ARG_NAME>", std::string("delegate"))
+        .Change("<ARG>", arg);
   } else if (type.GetUserDefinedType() == BaseType::UserType::ENUM) {
     parcel_write_code = ReplaceAll(CB_INTERFACE_METHOD_ENUM_UNIT_MAP_WRITE)
-         .Change("<ARG_NAME>", arg_name)
-         .Change("<ARG>", arg);
+        .Change("<ARG_NAME>", arg_name)
+        .Change("<ARG>", arg);
   } else if (type.IsUserDefinedType() ||
       type.GetMetaType() != nullptr ||
       type.GetKeyType() != nullptr) {
-    parcel_write_code = ReplaceAll(CB_INTERFACE_METHOD_USER_DEFINED_UNIT_MAP_WRITE)
-        .Change("<TYPE_NAME>", GetFullNameFromType(type, iface))
-        .Change("<ARG_NAME>", arg_name)
-        .Change("<ARG>", arg);
+    parcel_write_code =
+        ReplaceAll(CB_INTERFACE_METHOD_USER_DEFINED_UNIT_MAP_WRITE)
+            .Change("<TYPE_NAME>", GetFullNameFromType(type, iface))
+            .Change("<ARG_NAME>", arg_name)
+            .Change("<ARG>", arg);
     parcel_write_code += GetPrivateSharingString(type, iface, "port", arg);
   } else if (type.ToString() == "bundle") {
     parcel_write_code = ReplaceAll(CB_INTERFACE_METHOD_BUNDLE_UNIT_MAP_WRITE)
@@ -552,7 +555,7 @@ std::string CStubBodyGenerator::GenMethodUnitMapWriteBase(
         .Change("<ARG>", arg);
   }
 
-   return parcel_write_code;
+  return parcel_write_code;
 }
 // @see #CB_INTERFACE_METHOD_PARCEL_WRITE_PRE
 // @see #CB_INTERFACE_METHOD_PARCEL_WRITE_POST
@@ -660,17 +663,30 @@ std::string CStubBodyGenerator::GenPrivileges(const Declaration& decl) {
   return code;
 }
 
+// @see #CB_INTERFACE_METHOD_PRIVILEGE_CHECKER
+std::string CStubBodyGenerator::GenMethodPrivilegeChecker(
+    const Declaration& decl) {
+  auto privileges = GenPrivileges(decl);
+  if (privileges.empty())
+    return "return RPC_PORT_ERROR_NONE;" + NLine(1);
+
+  std::string sync =
+      (decl.GetMethodType() == Declaration::MethodType::SYNC) ?
+      "true" : "false";
+  std::string code(ReplaceAll(CB_INTERFACE_METHOD_PRIVILEGE_CHECKER)
+      .Change("<PRIVILEGES>", privileges)
+      .Change("<SYNC_TRUE_OR_FALSE>", sync));
+  return RemoveLine(code);
+}
+
 // @see #CB_INTERFACE_METHOD_PRIVILEGE_CHECKER_BASE
 void CStubBodyGenerator::GenInterfaceMethodPrivilegeCheckerBase(
     std::ofstream& stream, const Interface& iface, const Declaration& decl) {
-  std::string sync =
-      (decl.GetMethodType() == Declaration::MethodType::SYNC) ? "true" : "false";
   ReplaceAll(CB_INTERFACE_METHOD_PRIVILEGE_CHECKER_BASE)
       .Change("<PREFIX>", GetHandlePrefix())
       .Change("<NAME>", iface.GetID())
       .Change("<METHOD_NAME>", decl.GetID())
-      .Change("<PRIVILEGES>", GenPrivileges(decl))
-      .Change("<SYNC_TRUE_OR_FALSE>", sync)
+      .Change("<METHOD_PRIVILEGE_CHECKER>", GenMethodPrivilegeChecker(decl))
       .Transform([&](std::string code) { return SmartIndent(code); })
       .Out(stream);
 }
index c96d7272233ded27255bf3e8d4cd7c0c138188d7..39439d67166b1694cf2d4f8f7c37025389cf05aa 100644 (file)
@@ -83,6 +83,7 @@ class CStubBodyGenerator : public CBodyGeneratorBase {
   std::string GenDelegateParamsCheck(const Interface& iface,
       const Declaration& decl);
   std::string GenPrivileges(const Declaration& decl);
+  std::string GenMethodPrivilegeChecker(const Declaration& decl);
   std::string GenPrivilegeCheckers(const Interface& iface);
   std::string GenMethodHandlerArgsDecl(const Interface& iface,
       const Declaration& decl);
index 573045cb7092c5bb2c446e1edd51d4bccea5c42a..c94dfc3264e58ac77d4ffd872b575071e4b5d212 100644 (file)
@@ -1745,27 +1745,35 @@ int <PREFIX>_<NAME>_remote_exception_destroy(<PREFIX>_<NAME>_remote_exception_h
  * <PREFIX> The prefix of the interace.
  * <NAME> The name of the interface.
  * <METHOD_NAME> The name of the method of the interface.
- * <PRIVILEGES> The privileges of the method
- * <SYNC_TRUE_OR_FALSE> The flag if it's true, the stub should throw the remote exception.
+ * <METHOD_PRIVILEGE_CHECKER> The implementation of the method privilege checker.
  */
 constexpr const char CB_INTERFACE_METHOD_PRIVILEGE_CHECKER_BASE[] =
 R"__c_cb(
 static int __<PREFIX>_<NAME>_method_<METHOD_NAME>_privilege_checker(<PREFIX>_<NAME>_context_h h, bool *sync)
 {
-  static const char *privileges[] = {
-    <PRIVILEGES>
-  };
-
-  *sync = <SYNC_TRUE_OR_FALSE>;
-  for (int i = 0; i < ARRAY_SIZE(privileges); ++i) {
-    if (!g_hash_table_contains(h->privileges, privileges[i])) {
-      _E("Permission denied. %s : %s", h->sender, privileges[i]);
-      return RPC_PORT_ERROR_PERMISSION_DENIED;
-    }
-  }
+  <METHOD_PRIVILEGE_CHECKER>
+}
+)__c_cb";
 
-  return RPC_PORT_ERROR_NONE;
+/**
+ * <PRIVILEGES> The privileges of the method
+ * <SYNC_TRUE_OR_FALSE> The flag if it's true, the stub should throw the remote exception.
+ */
+constexpr const char CB_INTERFACE_METHOD_PRIVILEGE_CHECKER[] =
+R"__c_cb(
+static const char *privileges[] = {
+  <PRIVILEGES>
+};
+
+*sync = <SYNC_TRUE_OR_FALSE>;
+for (size_t i = 0; i < ARRAY_SIZE(privileges); ++i) {
+  if (!g_hash_table_contains(h->privileges, privileges[i])) {
+    _E("Permission denied. %s : %s", h->sender, privileges[i]);
+    return RPC_PORT_ERROR_PERMISSION_DENIED;
+  }
 }
+
+return RPC_PORT_ERROR_NONE;
 )__c_cb";
 
 /**