Try out old implementation of DFS algorithm
@@ -134,28 +134,25 @@ public class VulnerabilityCacheProcessorPlugin extends Plugin {
* @param path - helper argument for preliminary path.
* @return the list of paths for each vulnerable dependency.
*/
- private boolean getPathsToVulnerableNode(DirectedGraph graph, long source, long target,
- Set<Long> visited, Stack<Long> path) {
- visited.add(source);
+ private List<List<Long>> getPathsToVulnerableNode(DirectedGraph graph, long source, long target,
+ Set<Long> visited, List<Long> path, List<List<Long>> vulnerablePaths) {
+ if (path.isEmpty()) {
path.add(source);
-
- if (source == target) return true;
-
- if (!graph.containsVertex(source)) {
- path.pop();
- return false;
}
-
+ if (source == target) {
+ vulnerablePaths.add(new ArrayList<>(path));
+ return vulnerablePaths;
+ }
+ visited.add(source);
for (var node : graph.successors(source)) {
if (!visited.contains(node)) {
- if (getPathsToVulnerableNode(graph, node, target, visited, path)) {
- return true;
+ path.add(node);
+ getPathsToVulnerableNode(graph, node, target, visited, path, vulnerablePaths);
+ path.remove(node);
}
}
- }
-
- path.pop();
- return false;
+ visited.remove(source);
+ return vulnerablePaths;
}
/**
@@ -349,15 +346,13 @@ public class VulnerabilityCacheProcessorPlugin extends Plugin {
// Find all paths between any internal node and any vulnerable node in the graph
var vulnerablePaths = new ArrayList<List<Long>>();
- for (var internal : internalCallables) {
for (var vulnerable : vulnerabilities.keySet()) {
- Stack<Long> path = new Stack<>();
- var result = getPathsToVulnerableNode(graph, internal, vulnerable, new HashSet<>(), path);
- if (result) {
- logger.info("Path found for vulnerable: " + vulnerable.toString());
- vulnerablePaths.add(path);
- } else {
- logger.info("Path not found for vulnerable: " + vulnerable.toString());
+ for (var internal : internalCallables) {
+ List<List<Long>> listLists = getPathsToVulnerableNode(graph, internal, vulnerable, new HashSet<>(), new ArrayList<>(), new ArrayList<>());
+ if (listLists.size() > 0) {
+ vulnerablePaths.add(listLists.get(0));
+ logger.info("Vulnerable path was added to the set.");
+ break;
}
}
}
