Skip to content

Commit b0f7c79

Browse files
authored
fix: crash while initialize Enforcer without adapter bug (#78)
* fix: crash while initialize Enforcer without adapter bug Signed-off-by: chris <bigcat26@gmail.com> * fix: AddFunction is not working as expect Signed-off-by: chris <bigcat26@gmail.com> * fix: remove ApplyFunctionMap Signed-off-by: chris <bigcat26@gmail.com>
1 parent 1f2aa3d commit b0f7c79

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

casbin/enforcer.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
3939
// }()
4040

4141
this->func_map.scope = scope;
42-
this->func_map.LoadFunctionMap();
4342

4443
if(!this->enabled)
4544
return true;
@@ -69,6 +68,11 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
6968
}
7069
}
7170

71+
// apply function map to current scope.
72+
for(auto func: user_func_list){
73+
this->func_map.AddFunction(get<0>(func), get<1>(func), get<2>(func));
74+
}
75+
7276
unordered_map <string, int> p_int_tokens;
7377
for(int i = 0 ; i < this->model->m["p"].assertion_map["p"]->tokens.size() ; i++)
7478
p_int_tokens[this->model->m["p"].assertion_map["p"]->tokens[i]] = i;
@@ -78,7 +82,7 @@ bool Enforcer :: enforce(string matcher, Scope scope) {
7882
int policy_len = int(this->model->m["p"].assertion_map["p"]->policy.size());
7983

8084
vector <Effect> policy_effects(policy_len, Effect :: Indeterminate);
81-
vector <float> matcher_results(policy_len);
85+
vector <float> matcher_results(policy_len, 0.0);
8286

8387
if(policy_len != 0) {
8488
if(this->model->m["r"].assertion_map["r"]->tokens.size() != this->func_map.GetRLen())
@@ -200,7 +204,7 @@ Enforcer :: Enforcer(shared_ptr<Model> m, shared_ptr<Adapter> adapter) {
200204

201205
this->Initialize();
202206

203-
if (this->adapter->file_path != "") {
207+
if (this->adapter && this->adapter->file_path != "") {
204208
this->LoadPolicy();
205209
}
206210
}

casbin/enforcer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#ifndef CASBIN_CPP_ENFORCER
1818
#define CASBIN_CPP_ENFORCER
1919

20-
#include<memory>
20+
#include <tuple>
21+
#include <vector>
2122
#include "./rbac/role_manager.h"
2223
#include "./model/function.h"
2324
#include "./enforcer_interface.h"
@@ -30,6 +31,7 @@ class Enforcer : public IEnforcer{
3031
string model_path;
3132
shared_ptr<Model> model;
3233
FunctionMap func_map;
34+
vector <tuple<string, Function, Index>> user_func_list;
3335
shared_ptr<Effector> eft;
3436

3537
shared_ptr<Adapter> adapter;

casbin/management_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,5 +305,5 @@ bool Enforcer :: RemoveFilteredNamedGroupingPolicy(string p_type, int field_inde
305305

306306
// AddFunction adds a customized function.
307307
void Enforcer :: AddFunction(string name, Function function, Index nargs) {
308-
this->func_map.AddFunction(name, function, nargs);
308+
user_func_list.push_back(make_tuple(name, function, nargs));
309309
}

casbin/model/function.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ FunctionMap :: FunctionMap(){
2626
}
2727

2828
void FunctionMap :: ProcessFunctions(string expression){
29-
for(unordered_map<string, Function> :: iterator it = this->func_map.begin() ; it != this->func_map.end() ; it++){
30-
int index = int(expression.find((it->first)+"("));
29+
for(auto func: func_list){
30+
int index = int(expression.find(func+"("));
3131

3232
if(index != string::npos){
3333
int close_index = int(expression.find(")", index));
34-
int start = index + int(((it->first)+"(").length());
34+
int start = index + int((func+"(").length());
3535

3636
string function_params = expression.substr(start, close_index-start);
37-
FetchIdentifier(this->scope, it->first);
37+
FetchIdentifier(this->scope, func);
3838
vector<string> params = Split(function_params, ",");
3939

4040
for(int i=0;i<params.size();i++){
@@ -70,8 +70,8 @@ bool FunctionMap :: GetBooleanResult(){
7070

7171
// AddFunction adds an expression function.
7272
void FunctionMap :: AddFunction(string func_name, Function f, Index nargs) {
73-
func_map[func_name] = f;
74-
PushFunction(this->scope, f, func_name, nargs);
73+
func_list.push_back(func_name);
74+
PushFunction(scope, f, func_name, nargs);
7575
}
7676

7777
void FunctionMap :: AddFunctionPropToR(string identifier, Function func, Index nargs){
@@ -121,4 +121,4 @@ void FunctionMap :: LoadFunctionMap() {
121121
AddFunction("keyMatch3", KeyMatch3, 2);
122122
AddFunction("regexMatch", RegexMatch, 2);
123123
AddFunction("ipMatch", IPMatch, 2);
124-
}
124+
}

casbin/model/function.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#ifndef CASBIN_CPP_MODEL_FUNCTION
1818
#define CASBIN_CPP_MODEL_FUNCTION
1919

20-
#include <unordered_map>
20+
#include <list>
2121

2222
#include "../util/built_in_functions.h"
2323

@@ -26,7 +26,7 @@ using namespace std;
2626
class FunctionMap {
2727
public:
2828
Scope scope;
29-
unordered_map <string, Function> func_map;
29+
list <string> func_list;
3030

3131
FunctionMap();
3232

0 commit comments

Comments
 (0)