LUCENE-7831: CodecUtil should not seek to negative offsets.

This commit is contained in:
Adrien Grand 2017-05-16 18:31:57 +02:00
parent 851678388d
commit 9e1fcb0eb4
3 changed files with 23 additions and 1 deletions

View File

@ -93,7 +93,10 @@ Other
(Daniel Jelinski via Adrien Grand) (Daniel Jelinski via Adrien Grand)
======================= Lucene 6.7.0 ======================= ======================= Lucene 6.7.0 =======================
(No Changes)
Bug Fixes
* LUCENE-7831: CodecUtil should not seek to negative offsets. (Adrien Grand)
======================= Lucene 6.6.0 ======================= ======================= Lucene 6.6.0 =======================

View File

@ -331,6 +331,9 @@ public final class CodecUtil {
/** Retrieves the full footer from the provided {@link IndexInput}. This throws /** Retrieves the full footer from the provided {@link IndexInput}. This throws
* {@link CorruptIndexException} if this file does not have a valid footer. */ * {@link CorruptIndexException} if this file does not have a valid footer. */
public static byte[] readFooter(IndexInput in) throws IOException { public static byte[] readFooter(IndexInput in) throws IOException {
if (in.length() < footerLength()) {
throw new CorruptIndexException("misplaced codec footer (file truncated?): length=" + in.length() + " but footerLength==" + footerLength(), in);
}
in.seek(in.length() - footerLength()); in.seek(in.length() - footerLength());
validateFooter(in); validateFooter(in);
in.seek(in.length() - footerLength()); in.seek(in.length() - footerLength());
@ -516,6 +519,9 @@ public final class CodecUtil {
clone.seek(0); clone.seek(0);
ChecksumIndexInput in = new BufferedChecksumIndexInput(clone); ChecksumIndexInput in = new BufferedChecksumIndexInput(clone);
assert in.getFilePointer() == 0; assert in.getFilePointer() == 0;
if (in.length() < footerLength()) {
throw new CorruptIndexException("misplaced codec footer (file truncated?): length=" + in.length() + " but footerLength==" + footerLength(), input);
}
in.seek(in.length() - footerLength()); in.seek(in.length() - footerLength());
return checkFooter(in); return checkFooter(in);
} }

View File

@ -303,4 +303,17 @@ public class TestCodecUtil extends LuceneTestCase {
fakeChecksum.set((1L << 32) - 1); // ok fakeChecksum.set((1L << 32) - 1); // ok
CodecUtil.writeCRC(fakeOutput); CodecUtil.writeCRC(fakeOutput);
} }
public void testTruncatedFileThrowsCorruptIndexException() throws IOException {
RAMFile file = new RAMFile();
IndexOutput output = new RAMOutputStream(file, false);
output.close();
IndexInput input = new RAMInputStream("file", file);
CorruptIndexException e = expectThrows(CorruptIndexException.class,
() -> CodecUtil.checksumEntireFile(input));
assertEquals("misplaced codec footer (file truncated?): length=0 but footerLength==16 (resource=RAMInputStream(name=file))", e.getMessage());
e = expectThrows(CorruptIndexException.class,
() -> CodecUtil.retrieveChecksum(input));
assertEquals("misplaced codec footer (file truncated?): length=0 but footerLength==16 (resource=RAMInputStream(name=file))", e.getMessage());
}
} }