Permalink
Browse files

Wrap all recursion in pipes with StackOverflowException, and added an…

… option to output warnings.
  • Loading branch information...
me4502 committed May 21, 2017
1 parent 6467c8b commit 53407d92c8119a0c770a085470d8abc600f953c3
Showing with 27 additions and 8 deletions.
  1. +27 −8 src/main/java/com/sk89q/craftbook/mechanics/pipe/Pipes.java
@@ -119,10 +119,6 @@ else if (block.getRelative(face).getType() == Material.SIGN_POST && face != Bloc
}
private void searchNearbyPipes(Block block, Set<Vector> visitedPipes, List<ItemStack> items, int depth) {
if (depth > 1200) {
CraftBookPlugin.logger().info("Pipe of excessive length found at " + block.getLocation().toString());
return;
}
LinkedList<Block> searchQueue = new LinkedList<>();
//Enumerate the search queue.
@@ -199,8 +195,10 @@ else if (off.getType() == Material.THIN_GLASS || off.getType() == Material.STAIN
try {
searchNearbyPipes(bl, visitedPipes, items, depth + 1);
} catch (StackOverflowError e) {
CraftBookPlugin.logger().warning("Pipes encountered a StackOverflowError at position: " + bl.getLocation().toString() + ". "
+ "This occured at a depth of: " + depth);
if (warnWhenMassive) {
CraftBookPlugin.logger().warning("Pipes encountered a StackOverflowError at position: " + bl.getLocation().toString() + ". "
+ "This occured at a depth of: " + depth);
}
}
} else if (bl.getType() == Material.PISTON_BASE) {
@@ -261,7 +259,15 @@ else if (off.getType() == Material.THIN_GLASS || off.getType() == Material.STAIN
items.addAll(newItems);
}
if (!items.isEmpty()) searchNearbyPipes(block, visitedPipes, items, depth + 1);
try {
searchNearbyPipes(block, visitedPipes, items, depth + 1);
} catch (StackOverflowError e) {
if (warnWhenMassive) {
CraftBookPlugin.logger().warning("Pipes encountered a StackOverflowError at position: " + block.getLocation().toString() +
". "
+ "This occured at a depth of: " + depth);
}
}
} else if (bl.getType() == Material.DROPPER) {
ChangedSign sign = getSignOnPiston(bl);
@@ -298,7 +304,16 @@ else if (off.getType() == Material.THIN_GLASS || off.getType() == Material.STAIN
items.removeAll(filteredItems);
items.addAll(newItems);
if (!items.isEmpty()) searchNearbyPipes(block, visitedPipes, items, depth + 1);
if (!items.isEmpty()) {
try {
searchNearbyPipes(block, visitedPipes, items, depth + 1);
} catch (StackOverflowError e) {
if (warnWhenMassive) {
CraftBookPlugin.logger().warning("Pipes encountered a StackOverflowError at position: " + block.getLocation().toString()
+ ". This occured at a depth of: " + depth);
}
}
}
}
}
}
@@ -479,6 +494,7 @@ public void onPipeRequest(PipeRequestEvent event) {
private ItemInfo pipeInsulator;
private boolean pipeStackPerPull;
private boolean pipeRequireSign;
private boolean warnWhenMassive;
@Override
public void loadConfiguration (YAMLProcessor config, String path) {
@@ -494,5 +510,8 @@ public void loadConfiguration (YAMLProcessor config, String path) {
config.setComment(path + "require-sign", "Requires pipes to have a [Pipe] sign connected to them. This is the only way to require permissions to make pipes.");
pipeRequireSign = config.getBoolean(path + "require-sign", false);
config.setComment(path + "warn-when-too-large", "Prints a warning in the console when a pipe that's too large is found.");
warnWhenMassive = config.getBoolean(path + "warn-when-too-large", false);
}
}

0 comments on commit 53407d9

Please sign in to comment.