diff --git a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsReader.java index 08cf56c034c..f63468ef147 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsReader.java @@ -118,12 +118,12 @@ public final class CompressingStoredFieldsReader extends StoredFieldsReader { assert CodecUtil.headerLength(codecNameIdx) == indexStream.getFilePointer(); indexReader = new CompressingStoredFieldsIndexReader(indexStream, si); + long maxPointer = -1; + if (version >= VERSION_CHECKSUM) { maxPointer = indexStream.readVLong(); - assert maxPointer + CodecUtil.footerLength() == d.fileLength(fieldsStreamFN); CodecUtil.checkFooter(indexStream); } else { - maxPointer = d.fileLength(fieldsStreamFN); CodecUtil.checkEOF(indexStream); } indexStream.close(); @@ -131,6 +131,14 @@ public final class CompressingStoredFieldsReader extends StoredFieldsReader { // Open the data file and read metadata fieldsStream = d.openInput(fieldsStreamFN, context); + if (version >= VERSION_CHECKSUM) { + if (maxPointer + CodecUtil.footerLength() != fieldsStream.length()) { + throw new CorruptIndexException("Invalid fieldsStream maxPointer (file truncated?): maxPointer=" + maxPointer + ", length=" + fieldsStream.length()); + } + } else { + maxPointer = fieldsStream.length(); + } + this.maxPointer = maxPointer; final String codecNameDat = formatName + CODEC_SFX_DAT; final int fieldsVersion = CodecUtil.checkHeader(fieldsStream, codecNameDat, VERSION_START, VERSION_CURRENT); if (version != fieldsVersion) {