fix leaks caused by Files.list Stream not being closed

This commit is contained in:
Jason P. Leasure 2020-07-02 18:28:42 -04:00
parent b3ee8cacfa
commit eb5cc0097e
3 changed files with 45 additions and 30 deletions

View file

@ -27,6 +27,7 @@ import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.tools.*;
import javax.tools.JavaFileObject.Kind;
@ -952,7 +953,9 @@ public class GhidraSourceBundle extends GhidraBundle {
* @throws IOException if there's a problem listing files
*/
ClassMapper(Path directory) throws IOException {
classToClassFilesMap = Files.exists(directory) ? Files.list(directory)
if (Files.exists(directory)) {
try (Stream<Path> pathStream = Files.list(directory)) {
classToClassFilesMap = pathStream
.filter(f -> Files.isRegularFile(f) &&
f.getFileName().toString().endsWith(".class"))
.collect(groupingBy(f -> {
@ -964,7 +967,12 @@ public class GhidraSourceBundle extends GhidraBundle {
}
// drop ".class"
return fileName.substring(0, fileName.length() - 6);
})) : Collections.emptyMap();
}));
}
}
else {
classToClassFilesMap = Collections.emptyMap();
}
}
List<Path> findAndRemove(ResourceFile sourceFile) {

View file

@ -18,9 +18,11 @@ package ghidra.app.script;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import generic.jar.ResourceFile;
import ghidra.app.plugin.core.osgi.BundleHost;
@ -230,9 +232,8 @@ public class GhidraScriptUtil {
*/
@Deprecated
public static List<ResourceFile> getExplodedCompiledSourceBundlePaths() {
try {
return Files.list(BundleHost.getOsgiDir())
.filter(Files::isDirectory)
try (Stream<Path> pathStream = Files.list(BundleHost.getOsgiDir())) {
return pathStream.filter(Files::isDirectory)
.map(x -> new ResourceFile(x.toFile()))
.collect(Collectors.toList());
}

View file

@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.swing.*;
import javax.swing.table.TableModel;
@ -189,10 +190,12 @@ public abstract class AbstractGhidraScriptMgrPluginTest
protected void wipeUserScripts() throws IOException {
Path userScriptDir = java.nio.file.Paths.get(GhidraScriptUtil.USER_SCRIPTS_DIR);
for (Path p : (Iterable<Path>) Files.list(userScriptDir)::iterator) {
try (Stream<Path> pathStream = Files.list(userScriptDir)) {
for (Path p : (Iterable<Path>) pathStream::iterator) {
wipe(p);
}
}
}
//==================================================================================================
// Private Methods
@ -392,7 +395,8 @@ public abstract class AbstractGhidraScriptMgrPluginTest
assertNotNull(editor);
editorTextArea = (JTextArea) findComponentByName(editor.getComponent(), GhidraScriptEditorComponentProvider.EDITOR_COMPONENT_NAME);
editorTextArea = (JTextArea) findComponentByName(editor.getComponent(),
GhidraScriptEditorComponentProvider.EDITOR_COMPONENT_NAME);
assertNotNull(editorTextArea);
buffer = new StringBuffer(editorTextArea.getText());
@ -411,7 +415,8 @@ public abstract class AbstractGhidraScriptMgrPluginTest
// initialize our editor variable to the newly opened editor
editor = waitForComponentProvider(GhidraScriptEditorComponentProvider.class);
editorTextArea = (JTextArea) findComponentByName(editor.getComponent(), GhidraScriptEditorComponentProvider.EDITOR_COMPONENT_NAME);
editorTextArea = (JTextArea) findComponentByName(editor.getComponent(),
GhidraScriptEditorComponentProvider.EDITOR_COMPONENT_NAME);
waitForSwing();
@ -686,8 +691,8 @@ public abstract class AbstractGhidraScriptMgrPluginTest
"Contents of file on disk do not match that of the editor after performing " +
"a save operation: " + file);
printChars(expectedContents, fileText);
Assert
.fail("Contents of file on disk do not match that of the editor after performing " +
Assert.fail(
"Contents of file on disk do not match that of the editor after performing " +
"a save operation: " + file);
}
//
@ -849,8 +854,8 @@ public abstract class AbstractGhidraScriptMgrPluginTest
Map<ResourceFile, GhidraScriptEditorComponentProvider> editorMap = provider.getEditorMap();
GhidraScriptEditorComponentProvider fileEditor = editorMap.get(file);
final JTextArea textArea =
(JTextArea) findComponentByName(fileEditor.getComponent(), GhidraScriptEditorComponentProvider.EDITOR_COMPONENT_NAME);
final JTextArea textArea = (JTextArea) findComponentByName(fileEditor.getComponent(),
GhidraScriptEditorComponentProvider.EDITOR_COMPONENT_NAME);
assertNotNull(textArea);
final String[] box = new String[1];
@ -1494,7 +1499,8 @@ public abstract class AbstractGhidraScriptMgrPluginTest
protected JTextComponent grabScriptEditorTextArea() {
GhidraScriptEditorComponentProvider scriptEditor = grabScriptEditor();
JTextArea textArea = (JTextArea) findComponentByName(scriptEditor.getComponent(), GhidraScriptEditorComponentProvider.EDITOR_COMPONENT_NAME);
JTextArea textArea = (JTextArea) findComponentByName(scriptEditor.getComponent(),
GhidraScriptEditorComponentProvider.EDITOR_COMPONENT_NAME);
assertNotNull(textArea);
return textArea;
}