You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* JGraphT version: head (commit `4ec044c3e3078d6b837fcb04fad8cbc66f9fdc2f`)
* Java version (java -version)/platform:
openjdk version "17.0.8" 2023-07-18 LTS
OpenJDK Runtime Environment Corretto-17.0.8.7.1 (build 17.0.8+7-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.8.7.1 (build 17.0.8+7-LTS, mixed mode, sharing)
Issue
The documentation of ConnectivityInspector notes that it can be used as a graph listener to allow updating its results rather than recomputing when the graph changes. However, ConnectivityInspector does not ensure that connectedSets is not null when accessing it on updates, while some graph update methods set it to null in response to vertex or edge removal. As a result, the other update methods then cause a NullPointerException. This can be fixed by using lazyFindConnectedSets in the graph update methods rather than accessing connectedSets directly (I'll prepare a PR to make this change if we can confirm this issue).
Steps to reproduce (small coding example)
@TestpublicvoidtestConnectivityInspectorUpdatesOnVertexAdd() {
ListenableGraph<String, DefaultEdge> g = newDefaultListenableGraph<>(newDefaultDirectedGraph<>(DefaultEdge.class));
ConnectivityInspector<String, DefaultEdge> inspector = newConnectivityInspector<>(g);
g.addGraphListener(inspector);
g.addVertex("foo"); // NullPointerException here because connectedSets is not yet initializedassertEquals(inspector.connectedSets().size(), 1);
}
@TestpublicvoidtestConnectivityInspectorUpdatesOnVertexRemovalAndAdd() {
ListenableGraph<String, DefaultEdge> g = newDefaultListenableGraph<>(newDefaultDirectedGraph<>(DefaultEdge.class));
ConnectivityInspector<String, DefaultEdge> inspector = newConnectivityInspector<>(g);
g.addGraphListener(inspector);
inspector.connectedSets(); // this initializes `connectedSets` preventing the previous issue.g.addVertex("foo");
assertEquals(inspector.connectedSets().size(), 1);
// Removing a vertex resets connectedSets to `null`g.removeVertex("foo");
// Adding a vertex causes the now-null connectedSets to be accessed:g.addVertex("bar"); // NPE here
}
@TestpublicvoidtestConnectivityInspectorUpdatesOnEdgeRemovalAndAddition() {
ListenableGraph<String, DefaultEdge> g = newDefaultListenableGraph<>(newDefaultDirectedGraph<>(DefaultEdge.class));
ConnectivityInspector<String, DefaultEdge> inspector = newConnectivityInspector<>(g);
g.addGraphListener(inspector);
inspector.connectedSets();
g.addVertex("foo");
g.addVertex("bar");
assertEquals(inspector.connectedSets().size(), 2);
g.addEdge("foo", "bar");
g.removeEdge("foo", "bar"); // also resets connectedSets to `null`g.addEdge("foo", "bar"); // NPE hereassertEquals(inspector.connectedSets().size(), 1);
}
Expected behaviour
The above tests run successfully.
Other information
The text was updated successfully, but these errors were encountered:
Issue
The documentation of
ConnectivityInspector
notes that it can be used as a graph listener to allow updating its results rather than recomputing when the graph changes. However,ConnectivityInspector
does not ensure thatconnectedSets
is notnull
when accessing it on updates, while some graph update methods set it tonull
in response to vertex or edge removal. As a result, the other update methods then cause aNullPointerException
. This can be fixed by usinglazyFindConnectedSets
in the graph update methods rather than accessingconnectedSets
directly (I'll prepare a PR to make this change if we can confirm this issue).Steps to reproduce (small coding example)
Expected behaviour
The above tests run successfully.
Other information
The text was updated successfully, but these errors were encountered: