Browse Source

Fix wrongly computed offset in checksum (#69441)

Closes #69437
Tanguy Leroux 4 years ago
parent
commit
e7af85710f

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

@@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 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 @@ public abstract class BaseSearchableSnapshotIndexInput extends BufferedIndexInpu
             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 @@ public abstract class BaseSearchableSnapshotIndexInput extends BufferedIndexInpu
         }
         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