From 97253b31509ea3b965adb40e1e05e917bf4dd747 Mon Sep 17 00:00:00 2001 From: andsel Date: Thu, 7 Jan 2021 16:40:35 +0100 Subject: [PATCH] Fixed NPE due to TNode processing before CNode --- .../moquette/broker/subscriptions/CNode.java | 11 ++++++++++- .../moquette/broker/subscriptions/CTrie.java | 18 +++++++++--------- .../broker/subscriptions/DumpTreeVisitor.java | 2 +- .../moquette/broker/subscriptions/TNode.java | 10 ++++++++++ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/broker/src/main/java/io/moquette/broker/subscriptions/CNode.java b/broker/src/main/java/io/moquette/broker/subscriptions/CNode.java index 70c83576c..a356fc1dc 100644 --- a/broker/src/main/java/io/moquette/broker/subscriptions/CNode.java +++ b/broker/src/main/java/io/moquette/broker/subscriptions/CNode.java @@ -19,7 +19,7 @@ class CNode { - Token token; + private Token token; private List children; Set subscriptions; @@ -35,6 +35,14 @@ private CNode(Token token, List children, Set subscriptions this.children = new ArrayList<>(children); } + public Token getToken() { + return token; + } + + public void setToken(Token token) { + this.token = token; + } + boolean anyChildrenMatch(Token token) { for (INode iNode : children) { final CNode child = iNode.mainNode(); @@ -75,6 +83,7 @@ CNode copy() { public void add(INode newINode) { this.children.add(newINode); } + public void remove(INode node) { this.children.remove(node); } diff --git a/broker/src/main/java/io/moquette/broker/subscriptions/CTrie.java b/broker/src/main/java/io/moquette/broker/subscriptions/CTrie.java index 70e279f0b..1a64a8493 100644 --- a/broker/src/main/java/io/moquette/broker/subscriptions/CTrie.java +++ b/broker/src/main/java/io/moquette/broker/subscriptions/CTrie.java @@ -25,7 +25,7 @@ private enum Action { CTrie() { final CNode mainNode = new CNode(); - mainNode.token = ROOT; + mainNode.setToken(ROOT); this.root = new INode(mainNode); } @@ -48,14 +48,14 @@ enum NavigationAction { } private NavigationAction evaluate(Topic topic, CNode cnode) { - if (Token.MULTI.equals(cnode.token)) { + if (Token.MULTI.equals(cnode.getToken())) { return NavigationAction.MATCH; } if (topic.isEmpty()) { return NavigationAction.STOP; } final Token token = topic.headToken(); - if (!(Token.SINGLE.equals(cnode.token) || cnode.token.equals(token) || ROOT.equals(cnode.token))) { + if (!(Token.SINGLE.equals(cnode.getToken()) || cnode.getToken().equals(token) || ROOT.equals(cnode.getToken()))) { return NavigationAction.STOP; } return NavigationAction.GODEEP; @@ -67,6 +67,9 @@ public Set recursiveMatch(Topic topic) { private Set recursiveMatch(Topic topic, INode inode) { CNode cnode = inode.mainNode(); + if (cnode instanceof TNode) { + return Collections.emptySet(); + } NavigationAction action = evaluate(topic, cnode); if (action == NavigationAction.MATCH) { return cnode.subscriptions; @@ -74,10 +77,7 @@ private Set recursiveMatch(Topic topic, INode inode) { if (action == NavigationAction.STOP) { return Collections.emptySet(); } - if (cnode instanceof TNode) { - return Collections.emptySet(); - } - Topic remainingTopic = (ROOT.equals(cnode.token)) ? topic : topic.exceptHeadToken(); + Topic remainingTopic = (ROOT.equals(cnode.getToken())) ? topic : topic.exceptHeadToken(); Set subscriptions = new HashSet<>(); if (remainingTopic.isEmpty()) { subscriptions.addAll(cnode.subscriptions); @@ -134,7 +134,7 @@ private INode createPathRec(Topic topic, Subscription newSubscription) { if (!remainingTopic.isEmpty()) { INode inode = createPathRec(remainingTopic, newSubscription); CNode cnode = new CNode(); - cnode.token = topic.headToken(); + cnode.setToken(topic.headToken()); cnode.add(inode); return new INode(cnode); } else { @@ -144,7 +144,7 @@ private INode createPathRec(Topic topic, Subscription newSubscription) { private INode createLeafNodes(Token token, Subscription newSubscription) { CNode newLeafCnode = new CNode(); - newLeafCnode.token = token; + newLeafCnode.setToken(token); newLeafCnode.addSubscription(newSubscription); return new INode(newLeafCnode); diff --git a/broker/src/main/java/io/moquette/broker/subscriptions/DumpTreeVisitor.java b/broker/src/main/java/io/moquette/broker/subscriptions/DumpTreeVisitor.java index c2f6e647e..a6c077353 100644 --- a/broker/src/main/java/io/moquette/broker/subscriptions/DumpTreeVisitor.java +++ b/broker/src/main/java/io/moquette/broker/subscriptions/DumpTreeVisitor.java @@ -24,7 +24,7 @@ class DumpTreeVisitor implements CTrie.IVisitor { @Override public void visit(CNode node, int deep) { String indentTabs = indentTabs(deep); - s += indentTabs + (node.token == null ? "''" : node.token.toString()) + prettySubscriptions(node) + "\n"; + s += indentTabs + (node.getToken() == null ? "''" : node.getToken().toString()) + prettySubscriptions(node) + "\n"; } private String prettySubscriptions(CNode node) { diff --git a/broker/src/main/java/io/moquette/broker/subscriptions/TNode.java b/broker/src/main/java/io/moquette/broker/subscriptions/TNode.java index 7112c6c1b..6c073d3fd 100644 --- a/broker/src/main/java/io/moquette/broker/subscriptions/TNode.java +++ b/broker/src/main/java/io/moquette/broker/subscriptions/TNode.java @@ -17,6 +17,16 @@ class TNode extends CNode { + @Override + public Token getToken() { + throw new IllegalStateException("Can't be invoked on TNode"); + } + + @Override + public void setToken(Token token) { + throw new IllegalStateException("Can't be invoked on TNode"); + } + @Override INode childOf(Token token) { throw new IllegalStateException("Can't be invoked on TNode");