DeltaStream: Fix data corruption when reading large copies

If the copy instruction was larger than the input buffer given to us,
we copied the wrong part of the base stream during the next read().

This occurred on really big binary files where a copy instruction
of 64k wasn't unreasonable, but the caller's buffer was only 8192
bytes long.  We copied the first 8192 bytes correctly, but then
reseeked the base stream back to the start of the copy region on
the second read of 8192 bytes.  Instead of a sequence like ABCD
being read into the caller, we read AAAA.

Change-Id: I240a3f722a3eda1ce8ef5db93b380e3bceb1e201
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
Shawn O. Pearce 2010-09-06 10:06:37 -07:00
parent 693f454e71
commit 741659fed4
2 changed files with 21 additions and 2 deletions

View File

@ -249,12 +249,29 @@ private void assertValidState() throws IOException {
assertEquals(data.length, BinaryDelta.getResultSize(delta));
assertTrue(Arrays.equals(data, BinaryDelta.apply(base, delta)));
// Assert that a single bulk read produces the correct result.
//
byte[] act = new byte[data.length];
DeltaStream in = open();
assertEquals(data.length, in.getSize());
assertEquals(data.length, in.read(act));
assertEquals(-1, in.read());
assertTrue(Arrays.equals(data, act));
assertTrue("bulk read has same content", Arrays.equals(data, act));
// Assert that smaller tiny reads have the same result too.
//
act = new byte[data.length];
in = open();
int read = 0;
while (read < data.length) {
int n = in.read(act, read, 128);
if (n <= 0)
break;
read += n;
}
assertEquals(data.length, read);
assertEquals(-1, in.read());
assertTrue("small reads have same content", Arrays.equals(data, act));
}
private DeltaStream open() throws IOException {

View File

@ -215,7 +215,8 @@ public int read(byte[] buf, int off, int len) throws IOException {
if (n < 0)
throw new CorruptObjectException(
JGitText.get().baseLengthIncorrect);
baseOffset += n;
copyOffset += n;
baseOffset = copyOffset;
break;
case CMD_INSERT:
@ -225,6 +226,7 @@ public int read(byte[] buf, int off, int len) throws IOException {
case CMD_EOF:
return 0 < act ? act : -1;
default:
throw new CorruptObjectException(
JGitText.get().unsupportedCommand0);