Skip to content

Commit

Permalink
Merge pull request #1099 from Bryce7832/bug/resourceleak_bytecode
Browse files Browse the repository at this point in the history
Fixed resource leak in sootup.java.bytecode.frontend module
  • Loading branch information
swissiety authored Oct 25, 2024
2 parents 2cca700 + e52d729 commit 4298c6e
Showing 1 changed file with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import sootup.core.IdentifierFactory;
import sootup.core.frontend.ClassProvider;
import sootup.core.frontend.ResolveException;
import sootup.core.frontend.SootClassSource;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.model.SourceType;
import sootup.core.transform.BodyInterceptor;
Expand Down Expand Up @@ -82,7 +83,7 @@ public Optional<JavaSootClassSource> getClassSource(
@Nonnull ClassType classType, @Nonnull View view) {
JavaClassType klassType = (JavaClassType) classType;

ClassProvider classProvider = new AsmJavaClassProvider(view);
ClassProvider classProvider = getClassProvider(view);
Path filepath =
theFileSystem.getPath(
klassType.getFullyQualifiedName().replace('.', '/')
Expand Down Expand Up @@ -142,33 +143,44 @@ protected Stream<JavaSootClassSource> getClassSourcesInternal(
@Nonnull IdentifierFactory identifierFactory,
@Nonnull View view) {

ClassProvider classProvider = new AsmJavaClassProvider(view);
ClassProvider classProvider = getClassProvider(view);

String moduleInfoFilename =
JavaModuleIdentifierFactory.MODULE_INFO_FILE
+ classProvider.getHandledFileType().getExtensionWithDot();

final Path archiveRoot = theFileSystem.getPath("modules", moduleSignature.getModuleName());
try {

return Files.walk(archiveRoot)
.filter(
filePath ->
!Files.isDirectory(filePath)
&& filePath
.toString()
.endsWith(classProvider.getHandledFileType().getExtensionWithDot())
&& !filePath.toString().endsWith(moduleInfoFilename))
.flatMap(
p ->
StreamUtils.optionalToStream(
classProvider.createClassSource(this, p, fromPath(p, identifierFactory))))
.map(src -> (JavaSootClassSource) src);
try (Stream<Path> paths = Files.walk(archiveRoot)) {
// collect into a list and then return a stream, so we do not leak the Stream returned by
// Files.walk
List<JavaSootClassSource> javaSootClassSources =
paths
.filter(
filePath -> {
if (!Files.isDirectory(filePath)) {
String pathStr = filePath.toString();
return pathStr.endsWith(
classProvider.getHandledFileType().getExtensionWithDot())
&& !pathStr.endsWith(moduleInfoFilename);
}
return false;
})
.<SootClassSource>flatMap(
p ->
StreamUtils.optionalToStream(
classProvider.createClassSource(this, p, fromPath(p, identifierFactory))))
.map(src -> (JavaSootClassSource) src)
.collect(Collectors.toList());
return javaSootClassSources.stream();
} catch (IOException e) {
throw new ResolveException("Error loading module " + moduleSignature, archiveRoot, e);
}
}

protected ClassProvider getClassProvider(@Nonnull View view) {
return new AsmJavaClassProvider(view);
}

@Override
public @Nonnull Collection<JavaSootClassSource> getClassSources(@Nonnull View view) {

Expand Down

0 comments on commit 4298c6e

Please sign in to comment.