Skip to content

Commit e7af857

Browse files
authored
Fix wrongly computed offset in checksum (#69441)
Closes #69437
1 parent 22c880e commit e7af857

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/BaseSearchableSnapshotIndexInput.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.function.Predicate;
2525

2626
import static org.elasticsearch.index.store.checksum.ChecksumBlobContainerIndexInput.checksumToBytesArray;
27+
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsUtils.toIntBytes;
2728

2829
public abstract class BaseSearchableSnapshotIndexInput extends BufferedIndexInput {
2930

@@ -99,16 +100,21 @@ private boolean maybeReadChecksumFromFileInfo(ByteBuffer b) throws IOException {
99100
return false;
100101
}
101102
final long position = getFilePointer() + this.offset;
102-
if (position < fileInfo.length() - CodecUtil.footerLength()) {
103+
final long checksumPosition = fileInfo.length() - CodecUtil.footerLength();
104+
if (position < checksumPosition) {
103105
return false;
104106
}
105107
if (isClone) {
106108
return false;
107109
}
108110
boolean success = false;
109111
try {
112+
final int checksumOffset = toIntBytes(Math.subtractExact(position, checksumPosition));
113+
assert checksumOffset <= CodecUtil.footerLength() : checksumOffset;
114+
assert 0 <= checksumOffset : checksumOffset;
115+
110116
final byte[] checksum = checksumToBytesArray(fileInfo.checksum());
111-
b.put(checksum, CodecUtil.footerLength() - remaining, remaining);
117+
b.put(checksum, checksumOffset, remaining);
112118
success = true;
113119
} catch (NumberFormatException e) {
114120
// tests disable this optimisation by passing an invalid checksum

0 commit comments

Comments
 (0)