]> source.dussan.org Git - jgit.git/commitdiff
WorkTreeUpdater: Fix unclosed streams 80/195180/2
authorThomas Wolf <twolf@apache.org>
Sun, 14 Aug 2022 14:38:57 +0000 (16:38 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Sun, 14 Aug 2022 19:33:18 +0000 (21:33 +0200)
1. A TemporaryBuffer.LocalFile must be destroyed to ensure the
   temporary file gets deleted on disk.
2. TemporaryBuffer.openInputStream() may be used only after
   TemporaryBuffer.close().
3. The caller of DirCacheCheckout.getContent() is responsible for
   closing the OutputStream!

Change-Id: I46abb0fba27656a1026858e5783fc60d4738a45e
Signed-off-by: Thomas Wolf <twolf@apache.org>
org.eclipse.jgit/src/org/eclipse/jgit/util/WorkTreeUpdater.java

index fb0b33a042e997437ad63c712d24a0a17f13a6ae..f872f9fba5b43d4e71e663ed2453c9dd13720b1e 100644 (file)
@@ -554,22 +554,30 @@ public class WorkTreeUpdater implements Closeable {
                if (inCore) {
                        return;
                }
-               CheckoutMetadata checkoutMetadata = new CheckoutMetadata(streamType, smudgeCommand);
+               CheckoutMetadata metadata = new CheckoutMetadata(streamType,
+                               smudgeCommand);
                if (safeWrite) {
-                       try (org.eclipse.jgit.util.TemporaryBuffer buffer =
-                                       new org.eclipse.jgit.util.TemporaryBuffer.LocalFile(null)) {
-                               // Write to a buffer and copy to the file only if everything was fine.
-                               DirCacheCheckout.getContent(
-                                               repo, path, checkoutMetadata, resultStreamLoader, null, buffer);
-                               InputStream bufIn = buffer.openInputStream();
-                               Files.copy(bufIn, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
+                       // Write to a buffer and copy to the file only if everything was
+                       // fine.
+                       TemporaryBuffer buffer = new TemporaryBuffer.LocalFile(null);
+                       try {
+                               try (TemporaryBuffer buf = buffer) {
+                                       DirCacheCheckout.getContent(repo, path, metadata,
+                                                       resultStreamLoader, null, buf);
+                               }
+                               try (InputStream bufIn = buffer.openInputStream()) {
+                                       Files.copy(bufIn, file.toPath(),
+                                                       StandardCopyOption.REPLACE_EXISTING);
+                               }
+                       } finally {
+                               buffer.destroy();
                        }
                        return;
                }
-               OutputStream outputStream = new FileOutputStream(file);
-               DirCacheCheckout.getContent(
-                               repo, path, checkoutMetadata, resultStreamLoader, null, outputStream);
-
+               try (OutputStream outputStream = new FileOutputStream(file)) {
+                       DirCacheCheckout.getContent(repo, path, metadata,
+                                       resultStreamLoader, null, outputStream);
+               }
        }
 
        /**