Skip to content

Commit

Permalink
fix: fix memory leakage in DefaultRoleManager::Clear() and in Default…
Browse files Browse the repository at this point in the history
…RoleManager::CreateRole()(#119) (#158)

Signed-off-by: stonex <1479765922@qq.com>
  • Loading branch information
sheny1xuan authored Nov 1, 2021
1 parent 4ba0cda commit abaa217
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
36 changes: 18 additions & 18 deletions casbin/rbac/default_role_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

namespace casbin {

Role* Role :: NewRole(std::string name) {
Role* role = new Role;
std::shared_ptr<Role> Role :: NewRole(std::string name) {
auto role = std::make_shared<Role>();
role->name = name;
return role;
}

void Role :: AddRole(Role* role) {
void Role :: AddRole(std::shared_ptr<Role> role) {
for (int i = 0 ; i < this->roles.size() ; i++) {
if (this->roles[i]->name == role->name)
return;
Expand All @@ -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> role) {
for (int i = 0; i < roles.size();i++) {
if (roles[i]->name == role->name)
roles.erase(roles.begin()+i);
Expand Down Expand Up @@ -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
Expand All @@ -104,7 +104,7 @@ std::vector<std::string> Role :: GetRoles() {
bool DefaultRoleManager :: HasRole(std::string name) {
bool ok = false;
if (this->has_pattern){
for (std::unordered_map<std::string, Role*> :: 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;
}
Expand All @@ -115,8 +115,8 @@ bool DefaultRoleManager :: HasRole(std::string name) {
return ok;
}

Role* DefaultRoleManager :: CreateRole(std::string name) {
Role* role;
std::shared_ptr<Role> DefaultRoleManager :: CreateRole(std::string name) {
std::shared_ptr<Role> role;
bool ok = this->all_roles.find(name) != this->all_roles.end();
if (!ok) {
all_roles[name] = Role :: NewRole(name);
Expand All @@ -125,9 +125,9 @@ Role* DefaultRoleManager :: CreateRole(std::string name) {
role = all_roles[name];

if (this->has_pattern) {
for (std::unordered_map<std::string, Role*> :: 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<Role> role1;
bool ok1 = this->all_roles.find(it->first) != this->all_roles.end();
if (!ok1) {
all_roles[it->first] = Role :: NewRole(it->first);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -260,8 +260,8 @@ std::vector<std::string> DefaultRoleManager :: GetUsers(std::string name, std::v
throw CasbinRBACException("error: name does not exist");

std::vector<std::string> names;
for (std::unordered_map<std::string, Role*>::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);
}
Expand All @@ -285,7 +285,7 @@ void DefaultRoleManager :: PrintRoles() {
// LogUtil::SetLogger(*logger);

std::string text = this->all_roles.begin()->second->ToString();
std::unordered_map<std::string, Role*> :: iterator it = this->all_roles.begin();
auto it = this->all_roles.begin();
it++;
for ( ; it != this->all_roles.end() ; it++)
text += ", " + it->second->ToString();
Expand Down
12 changes: 6 additions & 6 deletions casbin/rbac/default_role_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ typedef bool (*MatchingFunc)(const std::string&, const std::string&);
class Role {

private:
std::vector<Role*> roles;
std::vector<std::shared_ptr<Role>> roles;

public:
std::string name;

static Role* NewRole(std::string name);
static std::shared_ptr<Role> NewRole(std::string name);

void AddRole(Role* role);
void AddRole(std::shared_ptr<Role> role);

void DeleteRole(Role* role);
void DeleteRole(std::shared_ptr<Role> role);

bool HasRole(std::string name, int hierarchy_level);

Expand All @@ -53,14 +53,14 @@ class Role {

class DefaultRoleManager : public RoleManager {
private:
std::unordered_map<std::string, Role*> all_roles;
std::unordered_map<std::string, std::shared_ptr<Role>> 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<Role> CreateRole(std::string name);

public:

Expand Down

0 comments on commit abaa217

Please sign in to comment.