From caffc53a170335ec8cef8aa85991a8daa2bcff24 Mon Sep 17 00:00:00 2001 From: Ben Liblit Date: Sat, 5 Aug 2023 17:26:32 -0400 Subject: [PATCH] Remember to close a test jar file The `instrumenter.nextClass()` call in this test helper method opens the jar file that is identified by `instrumentedJarLocation`. Previously, nothing subsequently closed that jar file. This open-file leak causes trouble on Windows: Windows refuses to delete an open file, which then causes post-test `@TempDir` cleanup to fail. Now we explicitly close `instrumenter`, which in turn closes the jar file, which in turn allows post-test `@TempDir` cleanup to succeed on Windows. --- .../core/tests/shrike/FloatingPointsTest.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/core/src/test/java/com/ibm/wala/core/tests/shrike/FloatingPointsTest.java b/core/src/test/java/com/ibm/wala/core/tests/shrike/FloatingPointsTest.java index 9ec6a63241..141b0ae0ac 100644 --- a/core/src/test/java/com/ibm/wala/core/tests/shrike/FloatingPointsTest.java +++ b/core/src/test/java/com/ibm/wala/core/tests/shrike/FloatingPointsTest.java @@ -150,16 +150,18 @@ public void emitTo(Output w) { } private void write() throws IllegalStateException, IOException, InvalidClassFileException { - // Write all modified classes - for (ClassInstrumenter ci2 : classInstrumenters) { - if (ci2.isChanged()) { - ClassWriter cw = ci2.emitClass(); - instrumenter.outputModifiedClass(ci2, cw); + try { + // Write all modified classes + for (ClassInstrumenter ci2 : classInstrumenters) { + if (ci2.isChanged()) { + ClassWriter cw = ci2.emitClass(); + instrumenter.outputModifiedClass(ci2, cw); + } } + } finally { + // Finally write the instrumented jar + instrumenter.close(); } - - // Finally write the instrumented jar - instrumenter.close(); } private void setValidationInstrumenter() throws IOException { @@ -169,11 +171,15 @@ private void setValidationInstrumenter() throws IOException { instrumenter.addInputJar(instrumentedJarLocation.toFile()); instrumenter.beginTraversal(); - // To be able to reuse all classes from shrike save them in a new list - classInstrumenters = new ArrayList<>(); - ClassInstrumenter ci = null; - while ((ci = instrumenter.nextClass()) != null) { - classInstrumenters.add(ci); + try { + // To be able to reuse all classes from shrike save them in a new list + classInstrumenters = new ArrayList<>(); + ClassInstrumenter ci = null; + while ((ci = instrumenter.nextClass()) != null) { + classInstrumenters.add(ci); + } + } finally { + instrumenter.close(); } }