summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse
diff options
context:
space:
mode:
authorIvan Frade <ifrade@google.com>2023-09-05 10:17:52 -0700
committerIvan Frade <ifrade@google.com>2023-09-05 10:30:23 -0700
commitf90f0717a0abe08a58dd92c9b05d3f6c7144d3d9 (patch)
treeee5810511b5e4f1b43fdc2f98ab332311d3a3d23 /org.eclipse.jgit/src/org/eclipse
parent13c8dacae55b8719d228b303570c369231f7c1c1 (diff)
downloadjgit-f90f0717a0abe08a58dd92c9b05d3f6c7144d3d9.tar.gz
jgit-f90f0717a0abe08a58dd92c9b05d3f6c7144d3d9.zip
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
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java12
1 files changed, 12 insertions, 0 deletions
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<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];