From f0ac43ce3d012ae66cb2e80ca60c6b9b749a2a8f Mon Sep 17 00:00:00 2001 From: Jamie Shiell Date: Sat, 6 Aug 2022 11:09:04 +0100 Subject: [PATCH] Fix potentially unsafe VFS recursion --- .../idea/checkstyle/actions/BaseAction.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/infernus/idea/checkstyle/actions/BaseAction.java b/src/main/java/org/infernus/idea/checkstyle/actions/BaseAction.java index 10c22d22..3858b7dc 100644 --- a/src/main/java/org/infernus/idea/checkstyle/actions/BaseAction.java +++ b/src/main/java/org/infernus/idea/checkstyle/actions/BaseAction.java @@ -6,7 +6,9 @@ import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileVisitor; import com.intellij.openapi.wm.ToolWindow; import org.infernus.idea.checkstyle.CheckStyleBundle; import org.infernus.idea.checkstyle.StaticScanner; @@ -17,6 +19,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Optional; +import java.util.concurrent.atomic.AtomicBoolean; import static java.util.Optional.ofNullable; import static org.infernus.idea.checkstyle.actions.ToolWindowAccess.actOnToolWindowPanel; @@ -79,14 +82,23 @@ protected StaticScanner staticScanner(final Project project) { } protected boolean containsAtLeastOneFile(@NotNull final VirtualFile... files) { - boolean result = false; + final var result = new AtomicBoolean(false); for (VirtualFile file : files) { - if ((file.isDirectory() && containsAtLeastOneFile(file.getChildren())) || (!file.isDirectory() && file - .isValid())) { - result = true; + VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor<>() { + @Override + public @NotNull Result visitFileEx(@NotNull final VirtualFile file) { + if (!file.isDirectory() && file.isValid()) { + result.set(true); + return SKIP_CHILDREN; + } + return CONTINUE; + } + }); + + if (result.get()) { break; } } - return result; + return result.get(); } }