Skip to content

Commit

Permalink
fix: fix potential null pointer issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sukidayou committed Nov 5, 2024
1 parent 6fc0c4e commit de143ed
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/casbin/jcasbin/persist/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ public static void loadPolicyLine(String line, Model model) {
}

String[] tokens = splitCommaDelimited(line);
for (int i = 0; i < tokens.length; i++) {
tokens[i] = tokens[i].trim();
}

String key = tokens[0];
String key = tokens[0].replaceAll("[^a-zA-Z0-9]", "");
String sec = key.substring(0, 1);
Assertion ast = model.model.get(sec).get(key);
List<String> policy = Arrays.asList(Arrays.copyOfRange(tokens, 1, tokens.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.casbin.jcasbin.util.Util.splitCommaDelimited;

/**
* FileAdapter is the file adapter for Casbin.
* It can load policy from file or save policy to file.
Expand Down Expand Up @@ -154,7 +157,8 @@ public void removePolicy(String sec, String ptype, List<String> rule) {
}
try {
List<String> lines = IOUtils.readLines(new FileInputStream(filePath), Charset.forName("UTF-8"));
lines.remove(ruleText);
int index = findLineIndex(sec,ptype,rule,lines);
if(index != -1)lines.remove(index);
savePolicyFile(String.join("\n", lines));
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -169,4 +173,50 @@ public void removePolicy(String sec, String ptype, List<String> rule) {
public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, String... fieldValues) {
throw new UnsupportedOperationException("not implemented");
}

/**
* Finds the index of a line in a list of lines that matches the policy.
* If no such line is found, the method returns -1.
*
* @param sec the section, "p" or "g".
* @param ptype the policy type, "p", "p2", .. or "g", "g2", ..
* @param rule the rule.
* @param lines A list of lines to search through.
* @return The index of the first matching line in `lines`, or -1 if no match is found.
*/
private int findLineIndex(String sec, String ptype, List<String> rule, List<String> lines) {
for(int i = 0;i < lines.size();i ++) {
String line = lines.get(i);

if ("".equals(line)) {
continue;
}
if (line.charAt(0) == '#') {
continue;
}

String[] tokens = splitCommaDelimited(line);
for (int j = 0; j < tokens.length; j++) {
tokens[j] = tokens[j].trim();
}

String key = tokens[0].replaceAll("[^a-zA-Z0-9]", "");
if(!key.equals(ptype)) {
continue;
}

String lineSec = key.substring(0, 1);
if(!lineSec.equals(sec)) {
continue;
}

List<String> policy = Arrays.asList(Arrays.copyOfRange(tokens, 1, tokens.length));
if(!policy.equals(rule)) {
continue;
}

return i;
}
return -1;
}
}

0 comments on commit de143ed

Please sign in to comment.