Skip to content

Commit abaa217

Browse files
authored
fix: fix memory leakage in DefaultRoleManager::Clear() and in DefaultRoleManager::CreateRole()(#119) (#158)
Signed-off-by: stonex <[email protected]>
1 parent 4ba0cda commit abaa217

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

casbin/rbac/default_role_manager.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525

2626
namespace casbin {
2727

28-
Role* Role :: NewRole(std::string name) {
29-
Role* role = new Role;
28+
std::shared_ptr<Role> Role :: NewRole(std::string name) {
29+
auto role = std::make_shared<Role>();
3030
role->name = name;
3131
return role;
3232
}
3333

34-
void Role :: AddRole(Role* role) {
34+
void Role :: AddRole(std::shared_ptr<Role> role) {
3535
for (int i = 0 ; i < this->roles.size() ; i++) {
3636
if (this->roles[i]->name == role->name)
3737
return;
@@ -40,7 +40,7 @@ void Role :: AddRole(Role* role) {
4040
this->roles.push_back(role);
4141
}
4242

43-
void Role :: DeleteRole(Role* role) {
43+
void Role :: DeleteRole(std::shared_ptr<Role> role) {
4444
for (int i = 0; i < roles.size();i++) {
4545
if (roles[i]->name == role->name)
4646
roles.erase(roles.begin()+i);
@@ -80,7 +80,7 @@ std::string Role :: ToString() {
8080
names += "(";
8181

8282
for (int i = 0; i < roles.size(); i ++) {
83-
Role* role = roles[i];
83+
auto role = roles[i];
8484
if (i == 0)
8585
names += role->name;
8686
else
@@ -104,7 +104,7 @@ std::vector<std::string> Role :: GetRoles() {
104104
bool DefaultRoleManager :: HasRole(std::string name) {
105105
bool ok = false;
106106
if (this->has_pattern){
107-
for (std::unordered_map<std::string, Role*> :: iterator it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){
107+
for (auto it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){
108108
if (this->matching_func(name, it->first))
109109
ok = true;
110110
}
@@ -115,8 +115,8 @@ bool DefaultRoleManager :: HasRole(std::string name) {
115115
return ok;
116116
}
117117

118-
Role* DefaultRoleManager :: CreateRole(std::string name) {
119-
Role* role;
118+
std::shared_ptr<Role> DefaultRoleManager :: CreateRole(std::string name) {
119+
std::shared_ptr<Role> role;
120120
bool ok = this->all_roles.find(name) != this->all_roles.end();
121121
if (!ok) {
122122
all_roles[name] = Role :: NewRole(name);
@@ -125,9 +125,9 @@ Role* DefaultRoleManager :: CreateRole(std::string name) {
125125
role = all_roles[name];
126126

127127
if (this->has_pattern) {
128-
for (std::unordered_map<std::string, Role*> :: iterator it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){
128+
for (auto it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){
129129
if (this->matching_func(name, it->first) && name!=it->first) {
130-
Role* role1;
130+
std::shared_ptr<Role> role1;
131131
bool ok1 = this->all_roles.find(it->first) != this->all_roles.end();
132132
if (!ok1) {
133133
all_roles[it->first] = Role :: NewRole(it->first);
@@ -178,8 +178,8 @@ void DefaultRoleManager :: AddLink(std::string name1, std::string name2, std::ve
178178
} else if (domain.size() > 1)
179179
throw CasbinRBACException("error: domain should be 1 parameter");
180180

181-
Role* role1 = this->CreateRole(name1);
182-
Role* role2 = this->CreateRole(name2);
181+
auto role1 = this->CreateRole(name1);
182+
auto role2 = this->CreateRole(name2);
183183
role1->AddRole(role2);
184184
}
185185

@@ -199,8 +199,8 @@ void DefaultRoleManager :: DeleteLink(std::string name1, std::string name2, std:
199199
if (!HasRole(name1) || !HasRole(name2))
200200
throw CasbinRBACException("error: name1 or name2 does not exist");
201201

202-
Role* role1 = this->CreateRole(name1);
203-
Role* role2 = this->CreateRole(name2);
202+
auto role1 = this->CreateRole(name1);
203+
auto role2 = this->CreateRole(name2);
204204
role1->DeleteRole(role2);
205205
}
206206

@@ -221,7 +221,7 @@ bool DefaultRoleManager :: HasLink(std::string name1, std::string name2, std::ve
221221
if (!HasRole(name1) || !HasRole(name2))
222222
return false;
223223

224-
Role* role1 = this->CreateRole(name1);
224+
auto role1 = this->CreateRole(name1);
225225
return role1->HasRole(name2, max_hierarchy_level);
226226
}
227227

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

262262
std::vector<std::string> names;
263-
for (std::unordered_map<std::string, Role*>::iterator it = this->all_roles.begin(); it != this->all_roles.end(); it++) {
264-
Role* role = it->second;
263+
for (auto it = this->all_roles.begin(); it != this->all_roles.end(); it++) {
264+
auto role = it->second;
265265
if (role->HasDirectRole(name))
266266
names.push_back(role->name);
267267
}
@@ -285,7 +285,7 @@ void DefaultRoleManager :: PrintRoles() {
285285
// LogUtil::SetLogger(*logger);
286286

287287
std::string text = this->all_roles.begin()->second->ToString();
288-
std::unordered_map<std::string, Role*> :: iterator it = this->all_roles.begin();
288+
auto it = this->all_roles.begin();
289289
it++;
290290
for ( ; it != this->all_roles.end() ; it++)
291291
text += ", " + it->second->ToString();

casbin/rbac/default_role_manager.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ typedef bool (*MatchingFunc)(const std::string&, const std::string&);
3131
class Role {
3232

3333
private:
34-
std::vector<Role*> roles;
34+
std::vector<std::shared_ptr<Role>> roles;
3535

3636
public:
3737
std::string name;
3838

39-
static Role* NewRole(std::string name);
39+
static std::shared_ptr<Role> NewRole(std::string name);
4040

41-
void AddRole(Role* role);
41+
void AddRole(std::shared_ptr<Role> role);
4242

43-
void DeleteRole(Role* role);
43+
void DeleteRole(std::shared_ptr<Role> role);
4444

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

@@ -53,14 +53,14 @@ class Role {
5353

5454
class DefaultRoleManager : public RoleManager {
5555
private:
56-
std::unordered_map<std::string, Role*> all_roles;
56+
std::unordered_map<std::string, std::shared_ptr<Role>> all_roles;
5757
bool has_pattern;
5858
int max_hierarchy_level;
5959
MatchingFunc matching_func;
6060

6161
bool HasRole(std::string name);
6262

63-
Role* CreateRole(std::string name);
63+
std::shared_ptr<Role> CreateRole(std::string name);
6464

6565
public:
6666

0 commit comments

Comments
 (0)