Pack: Buffer and close stream in #objectSizeIndex Pack#objectSizeIndex passed an unbuffered, unclosed FileInputStream to PackObjectSizeIndexLoader.load, which performs multiple small header and table reads. Wrap the stream in BufferedInputStream and close it via try-with-resources. Change-Id: I0c42a79bf28a5994b3fc0832af01ff312510550a
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackTest.java index 016a6af..6e9be4d 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/PackTest.java
@@ -16,6 +16,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -54,6 +55,7 @@ import org.eclipse.jgit.storage.file.WindowCacheConfig; import org.eclipse.jgit.transport.PackParser; import org.eclipse.jgit.transport.PackedObjectInfo; +import org.eclipse.jgit.util.FileUtils; import org.eclipse.jgit.util.IO; import org.eclipse.jgit.util.NB; import org.eclipse.jgit.util.TemporaryBuffer; @@ -322,6 +324,34 @@ public void testObjectSize() throws Exception { ObjectId.fromString("1111111111111111111111111111111111111111"))); } + @Test + public void testObjectSizeIndex_corruptFileClosesStream() throws Exception { + RevBlob aBlob = tr.blob(getRng().nextBytes(300)); + tr.branch("master").commit().add("A", aBlob).create(); + repo.getConfig().setInt(CONFIG_PACK_SECTION, null, + ConfigConstants.CONFIG_KEY_MIN_BYTES_OBJ_SIZE_INDEX, 0); + tr.packAndPrune(); + + Pack existingPack = repo.getObjectDatabase().getPacks().iterator() + .next(); + PackFile sizeIdxFile = existingPack.getPackFile() + .create(PackExt.OBJECT_SIZE_INDEX); + FileUtils.delete(sizeIdxFile); + try (FileOutputStream out = new FileOutputStream(sizeIdxFile)) { + out.write(new byte[] { 'b', 'a', 'd', '!' }); + } + + Pack freshPack = new Pack(repo.getConfig(), existingPack.getPackFile(), + null); + try { + assertThrows(IOException.class, freshPack::hasObjectSizeIndex); + FileUtils.delete(sizeIdxFile); + assertFalse(sizeIdxFile.exists()); + } finally { + freshPack.close(); + } + } + private static byte[] clone(int first, byte[] base) { byte[] r = new byte[base.length]; System.arraycopy(base, 1, r, 1, r.length - 1);
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/Pack.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/Pack.java index 8988b41..8699145 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/Pack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/Pack.java
@@ -19,11 +19,13 @@ import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PACKED_INDEX_GIT_USE_STRONGREFS; import static org.eclipse.jgit.internal.storage.pack.PackExt.OBJECT_SIZE_INDEX; +import java.io.BufferedInputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.io.InterruptedIOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; @@ -247,8 +249,11 @@ private PackObjectSizeIndex objectSizeIndex() throws IOException { attemptLoadObjSizeIdx = true; return null; } - sizeIdx = PackObjectSizeIndexLoader.load( - new FileInputStream(sizeIdxFile.getAbsoluteFile())); + try (FileInputStream fd = new FileInputStream( + sizeIdxFile.getAbsoluteFile()); + InputStream in = new BufferedInputStream(fd)) { + sizeIdx = PackObjectSizeIndexLoader.load(in); + } if (LOG.isDebugEnabled()) { LOG.debug(String.format( "Opening obj size index %s, size %.3f MB took %d ms", //$NON-NLS-1$