You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2021/07/12 20:33:23 UTC

[lucene] branch main updated: LUCENE-10019: Add extra checks as suggested by Adrien

This is an automated email from the ASF dual-hosted git repository.

uschindler pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new 15034f6  LUCENE-10019: Add extra checks as suggested by Adrien
15034f6 is described below

commit 15034f6c909a3473adcfbd936e533134e0dcfbbe
Author: Uwe Schindler <us...@apache.org>
AuthorDate: Mon Jul 12 22:33:10 2021 +0200

    LUCENE-10019: Add extra checks as suggested by Adrien
---
 lucene/core/src/java/org/apache/lucene/store/IndexOutput.java      | 7 +++++--
 .../src/test/org/apache/lucene/store/TestIndexOutputAlignment.java | 6 ++++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/store/IndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/IndexOutput.java
index 4041ebd..2a7eeae 100644
--- a/lucene/core/src/java/org/apache/lucene/store/IndexOutput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/IndexOutput.java
@@ -98,9 +98,12 @@ public abstract class IndexOutput extends DataOutput implements Closeable {
    * The alignment must be a power of 2.
    */
   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");
     }
-    return (offset - 1L + alignmentBytes) & (-alignmentBytes);
+    return Math.addExact(offset - 1L, alignmentBytes) & (-alignmentBytes);
   }
 }
diff --git a/lucene/core/src/test/org/apache/lucene/store/TestIndexOutputAlignment.java b/lucene/core/src/test/org/apache/lucene/store/TestIndexOutputAlignment.java
index 9ef7b33..c71a151 100644
--- a/lucene/core/src/test/org/apache/lucene/store/TestIndexOutputAlignment.java
+++ b/lucene/core/src/test/org/apache/lucene/store/TestIndexOutputAlignment.java
@@ -45,6 +45,8 @@ public class TestIndexOutputAlignment extends LuceneTestCase {
     assertEquals(val, IndexOutput.alignOffset(val - 1, Short.BYTES));
     // byte alignment never changes anything:
     assertEquals(val - 1, IndexOutput.alignOffset(val - 1, Byte.BYTES));
+
+    assertEquals(Long.MAX_VALUE, IndexOutput.alignOffset(Long.MAX_VALUE, Byte.BYTES));
   }
 
   public void testInvalidAlignments() {
@@ -53,6 +55,10 @@ public class TestIndexOutputAlignment extends LuceneTestCase {
     assertInvalidAligment(-2);
     assertInvalidAligment(6);
     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) {