Skip to content

Commit

Permalink
Merge pull request #2077 from Ilya-Skiba/bugfix/1881/not-close-zip-st…
Browse files Browse the repository at this point in the history
…ream

IOException happens during writing to jar
  • Loading branch information
StevenArzt authored May 2, 2024
2 parents fd77e1e + 16ac910 commit e4365f6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/main/java/soot/toDex/DexPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
Expand Down Expand Up @@ -313,7 +313,7 @@ private void printZip() throws IOException {
}

if (Options.v().output_jar()) {
// if we create JAR file, MANIFEST.MF is preferred
// if we create JAR file, MANIFEST.MF is preferred.
addManifest(outputZip, files);
}

Expand Down Expand Up @@ -404,8 +404,8 @@ private void addManifest(ZipOutputStream destination, Collection<File> dexFiles)
try (BufferedOutputStream bufOut = new BufferedOutputStream(destination)) {
manifest.write(bufOut);
bufOut.flush();
destination.closeEntry();
}
destination.closeEntry();
}

/**
Expand Down Expand Up @@ -1116,7 +1116,7 @@ protected Collection<Method> toMethods(SootClass clazz) {

/**
* Checks whether the given method shall be ignored, i.e., not written out to dex
*
*
* @param sm
* The method to check
* @return True to ignore the method while writing the dex file, false to write it out as normal
Expand All @@ -1127,7 +1127,7 @@ protected boolean isIgnored(SootMethod sm) {

/**
* Checks whether the given field shall be ignored, i.e., not written out to dex
*
*
* @param sf
* The field to check
* @return True to ignore the field while writing the dex file, false to write it out as normal
Expand Down Expand Up @@ -1293,9 +1293,9 @@ protected MethodImplementation toMethodImplementation(SootMethod m) {

/**
* Creates a statement visitor to build code for each statement.
*
*
* Allows subclasses to use own implementations
*
*
* @param belongingMethod
* the method
* @param arrayInitDetector
Expand All @@ -1308,7 +1308,7 @@ protected StmtVisitor buildStmtVisitor(SootMethod belongingMethod, DexArrayInitD

/**
* Writes out the information stored in the tags associated with the given statement
*
*
* @param builder
* The builder used to generate the Dalvik method implementation
* @param stmt
Expand Down Expand Up @@ -1397,7 +1397,7 @@ private void fixLongJumps(List<BuilderInstruction> instructions, LabelAssigner l
/*
* Assuming every instruction between this instruction and the jump target is a CONST_STRING instruction, how
* much could the distance increase?
*
*
* Because we only spend the effort to count the number of CONST_STRING instructions if there is a real
* chance that it changes the distance to overflow the allowed maximum.
*/
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/soot/PackManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,31 @@
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/

import com.google.common.io.Files;
import org.junit.Test;
import soot.options.Options;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


/**
* Some tests to disable jb transformers.
*
* @author Linghui Luo
*/
public class PackManagerTest {
Expand Down Expand Up @@ -122,6 +131,30 @@ public void testDisableUnusedLocalEliminatorInJBPhase() {
}
}

@Test
public void testWritingToJar() throws Exception {
setup();
File tempDir = Files.createTempDir();
Options.v().set_output_jar(true);
Options.v().set_output_dir(tempDir.getAbsolutePath());
Options.v().set_output_format(Options.output_format_dex);
Scene.v().loadNecessaryClasses();
PackManager.v().runBodyPacks();
PackManager.v().writeOutput();
assertEquals(1, tempDir.listFiles().length);
File targetJar = tempDir.listFiles()[0];
ZipFile jarRead = new ZipFile(targetJar.getAbsolutePath());
Enumeration<? extends ZipEntry> entries = jarRead.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().toLowerCase().endsWith("manifest.mf")) {
assertTrue(entry.getSize() > 0);
return;
}
}
fail("No Manifest entry found in " + targetJar.getAbsolutePath());
}

public static List<String> expectedBody(String... jimpleLines) {
return Stream.of(jimpleLines).collect(Collectors.toList());
}
Expand Down

0 comments on commit e4365f6

Please sign in to comment.