Skip to content

Commit

Permalink
Merge pull request soot-oss#1033 from Bryce7832/bug/fix_resource_leak…
Browse files Browse the repository at this point in the history
…s_bytecode&core

Fixed resource leaks in bytecode and core modules
  • Loading branch information
kadirayk authored Aug 20, 2024
2 parents 2f08679 + 4f00d42 commit 51d4f02
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 73 deletions.
3 changes: 2 additions & 1 deletion sootup.core/src/main/java/sootup/core/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ List<Path> compileJavaOTF(Path path) {
compiler.getTask(
null, fileManager, null, null, null, fileManager.getJavaFileObjects(javaName));
if (!task.call()) {
fileManager.close();
throw new IllegalArgumentException("could not compile source file.");
}

Expand All @@ -122,7 +123,7 @@ List<Path> compileJavaOTF(Path path) {
// pathOfCreatedClass.toFile().deleteOnExit();
compiledResults.add(pathOfCreatedClass);
}

fileManager.close();
return compiledResults;

} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
Expand Down Expand Up @@ -279,18 +280,18 @@ private void buildModuleForJar(@Nonnull Path jar) {
@Nonnull
public static String createModuleNameForAutomaticModule(@Nonnull Path path) {
// check if Automatic-Module-Name header exists in manifest file and use it if exists
try {
JarFile jar = new JarFile(path.toFile());

try (JarFile jar = new JarFile(path.toFile())) {
final String file = "META-INF/MANIFEST.MF";
JarEntry entry = (JarEntry) jar.getEntry(file);
if (entry != null) {
Manifest manifest = new Manifest(jar.getInputStream(entry));
Attributes attr = manifest.getMainAttributes();
try (InputStream jarStream = jar.getInputStream(entry)) {
Manifest manifest = new Manifest(jarStream);
Attributes attr = manifest.getMainAttributes();

String automaticModuleName = attr.getValue("Automatic-Module-Name");
if (automaticModuleName != null) {
return automaticModuleName;
String automaticModuleName = attr.getValue("Automatic-Module-Name");
if (automaticModuleName != null) {
return automaticModuleName;
}
}
}
} catch (IOException ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,8 @@ public Language getLanguage() {
}

public static boolean isMultiReleaseJar(Path path) {
try {
FileInputStream inputStream = new FileInputStream(path.toFile());
JarInputStream jarStream = new JarInputStream(inputStream);
try (FileInputStream inputStream = new FileInputStream(path.toFile());
JarInputStream jarStream = new JarInputStream(inputStream)) {
Manifest mf = jarStream.getManifest();

if (mf == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,33 +179,34 @@ static Path compile(List<Path> srcFiles) {
}

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(binDir));

File[] files = new File[srcFiles.size()];
srcFiles.stream().map(Path::toFile).collect(Collectors.toList()).toArray(files);
Iterable<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjects(files);

try (Writer writer = new StringWriter()) {
JavaCompiler.CompilationTask task =
compiler.getTask(writer, fileManager, null, null, null, javaFileObjects);

if (task.call()) {
/* collect all generated .class files
Set<JavaFileObject.Kind> clazzType = Collections.singleton(JavaFileObject.Kind.CLASS);
for (JavaFileObject jfo : fileManager.list(location, "", clazzType, true)) {
compiledFiles.add(Paths.get(jfo.toUri()));
}*/
if (!binDirCreated) {
// update modified timestamp of bin/
Files.setLastModifiedTime(binDirpath, FileTime.fromMillis(currentTimeMillis()));
try (StandardJavaFileManager fileManager =
compiler.getStandardFileManager(null, null, null)) {
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(binDir));

File[] files = new File[srcFiles.size()];
srcFiles.stream().map(Path::toFile).collect(Collectors.toList()).toArray(files);
Iterable<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjects(files);

try (Writer writer = new StringWriter()) {
JavaCompiler.CompilationTask task =
compiler.getTask(writer, fileManager, null, null, null, javaFileObjects);

if (task.call()) {
/* collect all generated .class files
Set<JavaFileObject.Kind> clazzType = Collections.singleton(JavaFileObject.Kind.CLASS);
for (JavaFileObject jfo : fileManager.list(location, "", clazzType, true)) {
compiledFiles.add(Paths.get(jfo.toUri()));
}*/
if (!binDirCreated) {
// update modified timestamp of bin/
Files.setLastModifiedTime(binDirpath, FileTime.fromMillis(currentTimeMillis()));
}
return binDir.toPath();
} else {
throw new IllegalArgumentException("Could not compile the given input.\n " + writer);
}
return binDir.toPath();
} else {
throw new IllegalArgumentException("Could not compile the given input.\n " + writer);
}
}

} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ private WarArchiveAnalysisInputLocation(

Path libDir = webInfPath.resolve("lib");
if (Files.exists(libDir)) {
try {
Files.walk(libDir)
try (Stream<Path> paths = Files.walk(libDir)) {
paths
.filter(f -> PathUtils.hasExtension(f, FileType.JAR))
.forEach(
f ->
Expand Down Expand Up @@ -432,7 +432,7 @@ public Optional<JavaSootClassSource> getClassSource(
*/
void extractWarFile(Path warFilePath, final Path destDirectory) {
int extractedSize = 0;
try {
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(warFilePath))) {
File dest = destDirectory.toFile();
if (!dest.exists()) {
if (!dest.mkdir()) {
Expand All @@ -442,7 +442,6 @@ void extractWarFile(Path warFilePath, final Path destDirectory) {
dest.deleteOnExit();
}

ZipInputStream zis = new ZipInputStream(Files.newInputStream(warFilePath));
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
Path filepath = destDirectory.resolve(zipEntry.getName());
Expand All @@ -457,45 +456,46 @@ void extractWarFile(Path warFilePath, final Path destDirectory) {
if (file.exists()) {
// compare contents -> does it contain the extracted war already?
int readBytesExistingFile;
final BufferedInputStream bis =
new BufferedInputStream(Files.newInputStream(file.toPath()));
byte[] bisBuf = new byte[4096];
while ((readBytesZip = zis.read(incomingValues)) != -1) {
if (extractedSize > maxAllowedBytesToExtract) {
throw new RuntimeException(
"The extracted warfile exceeds the size of "
+ maxAllowedBytesToExtract
+ " byte. Either the file is a big archive (-> increase PathBasedAnalysisInputLocation.WarArchiveInputLocation.maxAllowedBytesToExtract) or maybe it contains an archive bomb.");
}
readBytesExistingFile = bis.read(bisBuf, 0, readBytesZip);
if (readBytesExistingFile != readBytesZip) {
throw new RuntimeException(
"Can't extract File \""
+ file
+ "\" as it already exists and has a different size.");
} else if (!Arrays.equals(bisBuf, incomingValues)) {
throw new RuntimeException(
"Can't extract File \""
+ file
+ "\" as it already exists and has a different content which we can't override.");
try (final BufferedInputStream bis =
new BufferedInputStream(Files.newInputStream(file.toPath()))) {
byte[] bisBuf = new byte[4096];
while ((readBytesZip = zis.read(incomingValues)) != -1) {
if (extractedSize > maxAllowedBytesToExtract) {
throw new RuntimeException(
"The extracted warfile exceeds the size of "
+ maxAllowedBytesToExtract
+ " byte. Either the file is a big archive (-> increase PathBasedAnalysisInputLocation.WarArchiveInputLocation.maxAllowedBytesToExtract) or maybe it contains an archive bomb.");
}
readBytesExistingFile = bis.read(bisBuf, 0, readBytesZip);
if (readBytesExistingFile != readBytesZip) {
throw new RuntimeException(
"Can't extract File \""
+ file
+ "\" as it already exists and has a different size.");
} else if (!Arrays.equals(bisBuf, incomingValues)) {
throw new RuntimeException(
"Can't extract File \""
+ file
+ "\" as it already exists and has a different content which we can't override.");
}
extractedSize += readBytesZip;
}
extractedSize += readBytesZip;
}

} else {
BufferedOutputStream bos =
new BufferedOutputStream(Files.newOutputStream(file.toPath()));
while ((readBytesZip = zis.read(incomingValues)) != -1) {
if (extractedSize > maxAllowedBytesToExtract) {
throw new RuntimeException(
"The extracted warfile exceeds the size of "
+ maxAllowedBytesToExtract
+ " byte. Either the file is a big archive or maybe it contains an archive bomb.");
try (BufferedOutputStream bos =
new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
while ((readBytesZip = zis.read(incomingValues)) != -1) {
if (extractedSize > maxAllowedBytesToExtract) {
throw new RuntimeException(
"The extracted warfile exceeds the size of "
+ maxAllowedBytesToExtract
+ " byte. Either the file is a big archive or maybe it contains an archive bomb.");
}
bos.write(incomingValues, 0, readBytesZip);
extractedSize += readBytesZip;
}
bos.write(incomingValues, 0, readBytesZip);
extractedSize += readBytesZip;
}
bos.close();
}
}
zis.closeEntry();
Expand Down

0 comments on commit 51d4f02

Please sign in to comment.