From: Junghoon Park Date: Mon, 1 Jul 2019 02:25:04 +0000 (+0900) Subject: Use RAII idiom X-Git-Tag: accepted/tizen/unified/20190702.115009~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c0933adc774c48b3a1f291a520859847c3062cfe;p=platform%2Fcore%2Fappfw%2Frpc-port.git Use RAII idiom - Change raw pointers to smart pointers Change-Id: Ia81462b077d447763a73cd9584ad160b0c411170 Signed-off-by: Junghoon Park --- diff --git a/src/ac-internal.cc b/src/ac-internal.cc index a8c0f85..f8be409 100644 --- a/src/ac-internal.cc +++ b/src/ac-internal.cc @@ -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; } diff --git a/src/ac-internal.h b/src/ac-internal.h index 88bc8be..174f231 100644 --- a/src/ac-internal.h +++ b/src/ac-internal.h @@ -25,6 +25,7 @@ #include #include #include +#include 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_; + std::unique_ptr client_; + std::unique_ptr user_; }; int SetCache(const std::string& sender);