Skip to content

Commit

Permalink
feat: fix potential null pointer issue in loadPolicyLine() (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
sukidayou authored Nov 8, 2024
1 parent 6fc0c4e commit 7e28e62
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/main/java/org/casbin/jcasbin/persist/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;

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

Expand All @@ -40,7 +41,14 @@ public static void loadPolicyLine(String line, Model model) {

String key = tokens[0];
String sec = key.substring(0, 1);
Assertion ast = model.model.get(sec).get(key);
Map<String, Assertion> astMap = model.model.get(sec);
if(astMap == null) {
return;
}
Assertion ast = astMap.get(key);
if(ast == null) {
return;
}
List<String> policy = Arrays.asList(Arrays.copyOfRange(tokens, 1, tokens.length));
ast.policy.add(policy);
ast.policyIndex.put(policy.toString(), ast.policy.size() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ 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);
lines.removeIf(line -> line.replaceAll("\\s", "").equals(ruleText.replaceAll("\\s", "")));
savePolicyFile(String.join("\n", lines));
} catch (IOException e) {
e.printStackTrace();
Expand Down

0 comments on commit 7e28e62

Please sign in to comment.