Use RAII idiom 60/208860/1
authorJunghoon Park <jh9216.park@samsung.com>
Mon, 1 Jul 2019 02:25:04 +0000 (11:25 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Mon, 1 Jul 2019 02:25:04 +0000 (11:25 +0900)
- Change raw pointers to smart pointers

Change-Id: Ia81462b077d447763a73cd9584ad160b0c411170
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
src/ac-internal.cc
src/ac-internal.h

index a8c0f85..f8be409 100644 (file)
@@ -35,8 +35,6 @@
 namespace rpc_port {
 namespace internal {
 
-AccessController::~AccessController() {}
-
 void AccessController::AddPrivilege(std::string privilege) {
   privileges_.push_back(std::move(privilege));
 }
@@ -106,61 +104,46 @@ int AccessController::SetCache(const std::string& sender) {
   return -1;
 }
 
-AccessController::Cynara::Cynara() {
-  cynara_ = nullptr;
-  client_ = nullptr;
-  user_ = nullptr;
+AccessController::Cynara::Cynara()
+    : cynara_(nullptr, cynara_finish), client_(nullptr, std::free),
+    user_(nullptr, std::free) {
+  cynara* cynara_inst = nullptr;
 
-  if (cynara_initialize(&cynara_, NULL) != CYNARA_API_SUCCESS) {
+  if (cynara_initialize(&cynara_inst, NULL) != CYNARA_API_SUCCESS) {
     LOGE("cynara_initialize() is failed");
+  } else {
+    cynara_.reset(cynara_inst);
   }
 }
 
-AccessController::Cynara::~Cynara() {
-  if (client_)
-    free(client_);
-  if (user_)
-    free(user_);
-  if (cynara_)
-    cynara_finish(cynara_);
-}
-
 int AccessController::Cynara::FetchCredsFromDBus(GDBusConnection* connection,
     const char* sender) {
-  int ret;
-
-  if (client_) {
-    free(client_);
-     client_ = nullptr;
-  }
-
-  if (user_) {
-    free(user_);
-    user_ = nullptr;
-  }
-
-  ret = cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT,
-                                    &user_);
+  char* user = nullptr;
+  int ret = cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT,
+                                    &user);
   if (ret != CYNARA_API_SUCCESS) {
     LOGE("cynara_creds_gdbus_get_user() is failed : %d", ret);
     return -1;
   }
+  user_.reset(user);
 
+  char* client = nullptr;
   ret = cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT,
-                                      &client_);
+                                      &client);
   if (ret != CYNARA_API_SUCCESS) {
     LOGE("cynara_creds_gdbus_get_client() is failed : %d", ret);
     return -1;
   }
+  client_.reset(client);
 
-  LOGD("cred client : %s, cred user : %s", client_, user_);
+  LOGD("cred client : %s, cred user : %s", client_.get(), user_.get());
   return 0;
 }
 
 int AccessController::Cynara::Check(const std::string& privilege) const {
   LOGD("check privilege %s", privilege.c_str());
-  if (cynara_check(cynara_, client_, "", user_, privilege.c_str()) !=
-      CYNARA_API_ACCESS_ALLOWED) {
+  if (cynara_check(cynara_.get(), client_.get(), "", user_.get(),
+      privilege.c_str()) != CYNARA_API_ACCESS_ALLOWED) {
     LOGE("cynara_check() is not allowed : %s", privilege.c_str());
     return -1;
   }
index 88bc8be..174f231 100644 (file)
@@ -25,6 +25,7 @@
 #include <string>
 #include <vector>
 #include <map>
+#include <memory>
 
 namespace rpc_port {
 namespace internal {
@@ -32,7 +33,6 @@ namespace internal {
 class AccessController {
  public:
   explicit AccessController(bool trusted = false) : trusted_(trusted) {}
-  virtual ~AccessController();
 
   void AddPrivilege(std::string privilege);
   void SetTrusted(const bool trusted);
@@ -43,15 +43,14 @@ class AccessController {
   class Cynara {
    public:
      Cynara();
-     ~Cynara();
 
      int FetchCredsFromDBus(GDBusConnection *connection, const char *sender);
      int Check(const std::string& privilege) const;
 
    private:
-     cynara *cynara_;
-     char *client_;
-     char *user_;
+     std::unique_ptr<cynara, decltype(cynara_finish)*> cynara_;
+     std::unique_ptr<char, decltype(std::free)*> client_;
+     std::unique_ptr<char, decltype(std::free)*> user_;
   };
 
   int SetCache(const std::string& sender);