Skip to content

Commit

Permalink
Fix BlobCacheUtils.toPageAlignedSize
Browse files Browse the repository at this point in the history
toPageAlignedSize could round wrongly when the length did not fit
in an integer.
  • Loading branch information
henningandersen committed Apr 24, 2024
1 parent 0ac10c9 commit 47cebac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static int toIntBytes(long l) {
* Rounds the length up so that it is aligned on the next page size (defined by SharedBytes.PAGE_SIZE). For example
*/
public static long toPageAlignedSize(long length) {
int remainder = (int) length % SharedBytes.PAGE_SIZE;
int remainder = (int) (length % SharedBytes.PAGE_SIZE);
if (remainder > 0L) {
return length + (SharedBytes.PAGE_SIZE - remainder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*/
package org.elasticsearch.blobcache;

import org.elasticsearch.blobcache.shared.SharedBytes;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.Matchers;

import java.io.EOFException;
import java.nio.ByteBuffer;
Expand All @@ -19,4 +21,10 @@ public void testReadSafeThrows() {
final int remaining = randomIntBetween(1, 1025);
expectThrows(EOFException.class, () -> BlobCacheUtils.readSafe(BytesArray.EMPTY.streamInput(), buffer, 0, remaining));
}

public void testToPageAlignedSize() {
long value = randomLongBetween(0, Long.MAX_VALUE / 2);
long expected = ((value - 1) / SharedBytes.PAGE_SIZE + 1) * SharedBytes.PAGE_SIZE;
assertThat(BlobCacheUtils.toPageAlignedSize(value), Matchers.equalTo(expected));
}
}

0 comments on commit 47cebac

Please sign in to comment.