diff --git a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/BaseSearchableSnapshotIndexInput.java b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/BaseSearchableSnapshotIndexInput.java index 701d264b332f9..965b4e19381e0 100644 --- a/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/BaseSearchableSnapshotIndexInput.java +++ b/x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/BaseSearchableSnapshotIndexInput.java @@ -24,6 +24,7 @@ import java.util.function.Predicate; import static org.elasticsearch.index.store.checksum.ChecksumBlobContainerIndexInput.checksumToBytesArray; +import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsUtils.toIntBytes; public abstract class BaseSearchableSnapshotIndexInput extends BufferedIndexInput { @@ -99,7 +100,8 @@ private boolean maybeReadChecksumFromFileInfo(ByteBuffer b) throws IOException { return false; } final long position = getFilePointer() + this.offset; - if (position < fileInfo.length() - CodecUtil.footerLength()) { + final long checksumPosition = fileInfo.length() - CodecUtil.footerLength(); + if (position < checksumPosition) { return false; } if (isClone) { @@ -107,8 +109,12 @@ private boolean maybeReadChecksumFromFileInfo(ByteBuffer b) throws IOException { } boolean success = false; try { + final int checksumOffset = toIntBytes(Math.subtractExact(position, checksumPosition)); + assert checksumOffset <= CodecUtil.footerLength() : checksumOffset; + assert 0 <= checksumOffset : checksumOffset; + final byte[] checksum = checksumToBytesArray(fileInfo.checksum()); - b.put(checksum, CodecUtil.footerLength() - remaining, remaining); + b.put(checksum, checksumOffset, remaining); success = true; } catch (NumberFormatException e) { // tests disable this optimisation by passing an invalid checksum