From abaa2174f0a4c6df2a25c21c388c15939f472655 Mon Sep 17 00:00:00 2001 From: Stonex <43725202+sheny1xuan@users.noreply.github.com> Date: Tue, 2 Nov 2021 01:41:05 +0800 Subject: [PATCH] fix: fix memory leakage in DefaultRoleManager::Clear() and in DefaultRoleManager::CreateRole()(#119) (#158) Signed-off-by: stonex <1479765922@qq.com> --- casbin/rbac/default_role_manager.cpp | 36 ++++++++++++++-------------- casbin/rbac/default_role_manager.h | 12 +++++----- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/casbin/rbac/default_role_manager.cpp b/casbin/rbac/default_role_manager.cpp index f902e70e..11d70467 100644 --- a/casbin/rbac/default_role_manager.cpp +++ b/casbin/rbac/default_role_manager.cpp @@ -25,13 +25,13 @@ namespace casbin { -Role* Role :: NewRole(std::string name) { - Role* role = new Role; +std::shared_ptr Role :: NewRole(std::string name) { + auto role = std::make_shared(); role->name = name; return role; } -void Role :: AddRole(Role* role) { +void Role :: AddRole(std::shared_ptr role) { for (int i = 0 ; i < this->roles.size() ; i++) { if (this->roles[i]->name == role->name) return; @@ -40,7 +40,7 @@ void Role :: AddRole(Role* role) { this->roles.push_back(role); } -void Role :: DeleteRole(Role* role) { +void Role :: DeleteRole(std::shared_ptr role) { for (int i = 0; i < roles.size();i++) { if (roles[i]->name == role->name) roles.erase(roles.begin()+i); @@ -80,7 +80,7 @@ std::string Role :: ToString() { names += "("; for (int i = 0; i < roles.size(); i ++) { - Role* role = roles[i]; + auto role = roles[i]; if (i == 0) names += role->name; else @@ -104,7 +104,7 @@ std::vector Role :: GetRoles() { bool DefaultRoleManager :: HasRole(std::string name) { bool ok = false; if (this->has_pattern){ - for (std::unordered_map :: iterator it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){ + for (auto it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){ if (this->matching_func(name, it->first)) ok = true; } @@ -115,8 +115,8 @@ bool DefaultRoleManager :: HasRole(std::string name) { return ok; } -Role* DefaultRoleManager :: CreateRole(std::string name) { - Role* role; +std::shared_ptr DefaultRoleManager :: CreateRole(std::string name) { + std::shared_ptr role; bool ok = this->all_roles.find(name) != this->all_roles.end(); if (!ok) { all_roles[name] = Role :: NewRole(name); @@ -125,9 +125,9 @@ Role* DefaultRoleManager :: CreateRole(std::string name) { role = all_roles[name]; if (this->has_pattern) { - for (std::unordered_map :: iterator it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){ + for (auto it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){ if (this->matching_func(name, it->first) && name!=it->first) { - Role* role1; + std::shared_ptr role1; bool ok1 = this->all_roles.find(it->first) != this->all_roles.end(); if (!ok1) { all_roles[it->first] = Role :: NewRole(it->first); @@ -178,8 +178,8 @@ void DefaultRoleManager :: AddLink(std::string name1, std::string name2, std::ve } else if (domain.size() > 1) throw CasbinRBACException("error: domain should be 1 parameter"); - Role* role1 = this->CreateRole(name1); - Role* role2 = this->CreateRole(name2); + auto role1 = this->CreateRole(name1); + auto role2 = this->CreateRole(name2); role1->AddRole(role2); } @@ -199,8 +199,8 @@ void DefaultRoleManager :: DeleteLink(std::string name1, std::string name2, std: if (!HasRole(name1) || !HasRole(name2)) throw CasbinRBACException("error: name1 or name2 does not exist"); - Role* role1 = this->CreateRole(name1); - Role* role2 = this->CreateRole(name2); + auto role1 = this->CreateRole(name1); + auto role2 = this->CreateRole(name2); role1->DeleteRole(role2); } @@ -221,7 +221,7 @@ bool DefaultRoleManager :: HasLink(std::string name1, std::string name2, std::ve if (!HasRole(name1) || !HasRole(name2)) return false; - Role* role1 = this->CreateRole(name1); + auto role1 = this->CreateRole(name1); return role1->HasRole(name2, max_hierarchy_level); } @@ -260,8 +260,8 @@ std::vector DefaultRoleManager :: GetUsers(std::string name, std::v throw CasbinRBACException("error: name does not exist"); std::vector names; - for (std::unordered_map::iterator it = this->all_roles.begin(); it != this->all_roles.end(); it++) { - Role* role = it->second; + for (auto it = this->all_roles.begin(); it != this->all_roles.end(); it++) { + auto role = it->second; if (role->HasDirectRole(name)) names.push_back(role->name); } @@ -285,7 +285,7 @@ void DefaultRoleManager :: PrintRoles() { // LogUtil::SetLogger(*logger); std::string text = this->all_roles.begin()->second->ToString(); - std::unordered_map :: iterator it = this->all_roles.begin(); + auto it = this->all_roles.begin(); it++; for ( ; it != this->all_roles.end() ; it++) text += ", " + it->second->ToString(); diff --git a/casbin/rbac/default_role_manager.h b/casbin/rbac/default_role_manager.h index 39b14b13..d03f74f6 100644 --- a/casbin/rbac/default_role_manager.h +++ b/casbin/rbac/default_role_manager.h @@ -31,16 +31,16 @@ typedef bool (*MatchingFunc)(const std::string&, const std::string&); class Role { private: - std::vector roles; + std::vector> roles; public: std::string name; - static Role* NewRole(std::string name); + static std::shared_ptr NewRole(std::string name); - void AddRole(Role* role); + void AddRole(std::shared_ptr role); - void DeleteRole(Role* role); + void DeleteRole(std::shared_ptr role); bool HasRole(std::string name, int hierarchy_level); @@ -53,14 +53,14 @@ class Role { class DefaultRoleManager : public RoleManager { private: - std::unordered_map all_roles; + std::unordered_map> all_roles; bool has_pattern; int max_hierarchy_level; MatchingFunc matching_func; bool HasRole(std::string name); - Role* CreateRole(std::string name); + std::shared_ptr CreateRole(std::string name); public: