]> source.dussan.org Git - jgit.git/commitdiff
CommitGraphWriter: Assert written bytes 23/204123/2
authorIvan Frade <ifrade@google.com>
Tue, 5 Sep 2023 17:17:52 +0000 (10:17 -0700)
committerIvan Frade <ifrade@google.com>
Tue, 5 Sep 2023 17:30:23 +0000 (10:30 -0700)
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

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java

index bab262231f69682d18ab67a371a634452e715280..afbd7e654a13df1881e176e510ae12b6d1e2649d 100644 (file)
@@ -122,6 +122,7 @@ public class CommitGraphWriter {
                }
 
                List<ChunkHeader> 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<ChunkHeader> 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];