From: Ivan Frade Date: Tue, 5 Sep 2023 17:17:52 +0000 (-0700) Subject: CommitGraphWriter: Assert written bytes X-Git-Tag: v6.8.0.202310031045-m1~73 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fchanges%2F23%2F204123%2F2;p=jgit.git CommitGraphWriter: Assert written bytes The final size of the commit-graph is known before-hand. As a safety-net, assert the written size matches the expected value. Change-Id: Ib0828a7cce5bacb33f6325ee3910f4eebd95eb8c --- diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java index bab262231f..afbd7e654a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java @@ -122,6 +122,7 @@ public class CommitGraphWriter { } List chunks = createChunks(stats); + long expectedSize = calculateExpectedSize(chunks); long writeCount = 256 + 2 * graphCommits.size() + graphCommits.getExtraEdgeCnt(); monitor.beginTask( @@ -135,6 +136,11 @@ public class CommitGraphWriter { writeChunkLookup(out, chunks); writeChunks(monitor, out, chunks); writeCheckSum(out); + if (expectedSize != out.length()) { + throw new IllegalStateException(String.format( + "Commit-graph: expected %d bytes but out has %d bytes", //$NON-NLS-1$ + expectedSize, out.length())); + } } catch (InterruptedIOException e) { throw new IOException(JGitText.get().commitGraphWritingCancelled, e); @@ -168,6 +174,12 @@ public class CommitGraphWriter { return chunks; } + private static long calculateExpectedSize(List chunks) { + int chunkLookup = (chunks.size() + 1) * CHUNK_LOOKUP_WIDTH; + long chunkContent = chunks.stream().mapToLong(c -> c.size).sum(); + return /* header */ 8 + chunkLookup + chunkContent + /* CRC */ 20; + } + private void writeHeader(CancellableDigestOutputStream out, int numChunks) throws IOException { byte[] headerBuffer = new byte[8];