From f90f0717a0abe08a58dd92c9b05d3f6c7144d3d9 Mon Sep 17 00:00:00 2001 From: Ivan Frade Date: Tue, 5 Sep 2023 10:17:52 -0700 Subject: [PATCH] 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 --- .../storage/commitgraph/CommitGraphWriter.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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]; -- 2.39.5