From 2b7fa04dad12c57389281076d54085e536916f19 Mon Sep 17 00:00:00 2001 From: Minh Thai Date: Wed, 13 Feb 2019 12:16:42 -0800 Subject: Fix bug in copyPackBypassCache's skip 'PACK' header logic Bug caused the pack to be 12 bytes short when cold cache. Also added test for copyPackAsIs method. Change-Id: Idf8fb0e50d1215245d4b032e2e00df4b218c115f Signed-off-by: Minh Thai --- .../jgit/internal/storage/dfs/DfsPackFile.java | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'org.eclipse.jgit') diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackFile.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackFile.java index 7d891b5230..5b574e49a9 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackFile.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackFile.java @@ -464,6 +464,9 @@ public final class DfsPackFile extends BlockBasedFile { while (0 < remaining) { DfsBlock b = cache.getOrLoad(this, position, ctx, () -> rc); int ptr = (int) (position - b.start); + if (b.size() <= ptr) { + throw packfileIsTruncated(); + } int n = (int) Math.min(b.size() - ptr, remaining); b.write(out, position, n); position += n; @@ -481,6 +484,9 @@ public final class DfsPackFile extends BlockBasedFile { DfsBlock b = cache.get(key, alignToBlock(position)); if (b != null) { int ptr = (int) (position - b.start); + if (b.size() <= ptr) { + throw packfileIsTruncated(); + } int n = (int) Math.min(b.size() - ptr, remaining); b.write(out, position, n); position += n; @@ -490,23 +496,18 @@ public final class DfsPackFile extends BlockBasedFile { continue; } + // Need to skip the 'PACK' header for the first read + int ptr = packHeadSkipped ? 0 : 12; buf.position(0); - int n = read(rc, buf); - if (n <= 0) { + int bufLen = read(rc, buf); + if (bufLen <= ptr) { throw packfileIsTruncated(); - } else if (n > remaining) { - n = (int) remaining; - } - - if (!packHeadSkipped) { - // Need skip the 'PACK' header for the first read - out.write(buf.array(), 12, n - 12); - packHeadSkipped = true; - } else { - out.write(buf.array(), 0, n); } + int n = (int) Math.min(bufLen - ptr, remaining); + out.write(buf.array(), ptr, n); position += n; remaining -= n; + packHeadSkipped = true; } return position; } -- cgit v1.2.3