mirror of https://github.com/apache/lucene.git
LUCENE-10019: Add extra checks as suggested by Adrien
This commit is contained in:
parent
08e61a2201
commit
15034f6c90
|
@ -98,9 +98,12 @@ public abstract class IndexOutput extends DataOutput implements Closeable {
|
||||||
* The alignment must be a power of 2.
|
* The alignment must be a power of 2.
|
||||||
*/
|
*/
|
||||||
public static final long alignOffset(long offset, int alignmentBytes) {
|
public static final long alignOffset(long offset, int alignmentBytes) {
|
||||||
if (1 != Integer.bitCount(alignmentBytes)) {
|
if (offset < 0L) {
|
||||||
|
throw new IllegalArgumentException("Offset must be positive");
|
||||||
|
}
|
||||||
|
if (1 != Integer.bitCount(alignmentBytes) || alignmentBytes < 0) {
|
||||||
throw new IllegalArgumentException("Alignment must be a power of 2");
|
throw new IllegalArgumentException("Alignment must be a power of 2");
|
||||||
}
|
}
|
||||||
return (offset - 1L + alignmentBytes) & (-alignmentBytes);
|
return Math.addExact(offset - 1L, alignmentBytes) & (-alignmentBytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,8 @@ public class TestIndexOutputAlignment extends LuceneTestCase {
|
||||||
assertEquals(val, IndexOutput.alignOffset(val - 1, Short.BYTES));
|
assertEquals(val, IndexOutput.alignOffset(val - 1, Short.BYTES));
|
||||||
// byte alignment never changes anything:
|
// byte alignment never changes anything:
|
||||||
assertEquals(val - 1, IndexOutput.alignOffset(val - 1, Byte.BYTES));
|
assertEquals(val - 1, IndexOutput.alignOffset(val - 1, Byte.BYTES));
|
||||||
|
|
||||||
|
assertEquals(Long.MAX_VALUE, IndexOutput.alignOffset(Long.MAX_VALUE, Byte.BYTES));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testInvalidAlignments() {
|
public void testInvalidAlignments() {
|
||||||
|
@ -53,6 +55,10 @@ public class TestIndexOutputAlignment extends LuceneTestCase {
|
||||||
assertInvalidAligment(-2);
|
assertInvalidAligment(-2);
|
||||||
assertInvalidAligment(6);
|
assertInvalidAligment(6);
|
||||||
assertInvalidAligment(43);
|
assertInvalidAligment(43);
|
||||||
|
assertInvalidAligment(Integer.MIN_VALUE);
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> IndexOutput.alignOffset(-1L, 1));
|
||||||
|
assertThrows(ArithmeticException.class, () -> IndexOutput.alignOffset(Long.MAX_VALUE, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertInvalidAligment(int size) {
|
private static void assertInvalidAligment(int size) {
|
||||||
|
|
Loading…
Reference in New Issue