You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ch...@apache.org on 2025/01/20 09:33:38 UTC
(lucene) branch main updated: Avoid overflow in index input slices invariant checks (#14126)
This is an automated email from the ASF dual-hosted git repository.
chegar 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 7e20d5b3aad Avoid overflow in index input slices invariant checks (#14126)
7e20d5b3aad is described below
commit 7e20d5b3aad13739a2ae9fa36774e68ccae43ac4
Author: Chris Hegarty <62...@users.noreply.github.com>
AuthorDate: Mon Jan 20 10:33:31 2025 +0100
Avoid overflow in index input slices invariant checks (#14126)
This commit avoids overflow in index input slices invariant checks.
While not a problem in practice, this can lead to more obscure failures which are harder to diagnose. Reworking the invariant checks to avoid the potential to overflow is trivial. Existing tests cover the most cases, while a single new scenario covered the overflow case.
---
lucene/CHANGES.txt | 3 +++
.../core/src/java/org/apache/lucene/store/BufferedIndexInput.java | 2 +-
.../core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java | 2 +-
lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java | 2 +-
.../src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java | 2 +-
lucene/misc/src/java/org/apache/lucene/misc/store/RAFDirectory.java | 2 +-
.../java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java | 6 ++++++
.../org/apache/lucene/tests/store/SerialIOCountingDirectory.java | 2 +-
8 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c2b37a94f89..39d7403a11c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -84,6 +84,9 @@ Bug Fixes
* GITHUB#14123: SortingCodecReader NPE when segment has no (points, vectors, etc...) (Mike Sokolov)
+* GITHUB#14126: Avoid overflow in index input slices invariant checks
+ (Chris Hegarty)
+
Other
---------------------
diff --git a/lucene/core/src/java/org/apache/lucene/store/BufferedIndexInput.java b/lucene/core/src/java/org/apache/lucene/store/BufferedIndexInput.java
index 1738259fa2f..cd47500a2df 100644
--- a/lucene/core/src/java/org/apache/lucene/store/BufferedIndexInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/BufferedIndexInput.java
@@ -401,7 +401,7 @@ public abstract class BufferedIndexInput extends IndexInput implements RandomAcc
? base.toString()
: (base.toString() + " [slice=" + sliceDescription + "]"),
BufferedIndexInput.BUFFER_SIZE);
- if (offset < 0 || length < 0 || offset + length > base.length()) {
+ if ((length | offset) < 0 || length > base.length() - offset) {
throw new IllegalArgumentException(
"slice() " + sliceDescription + " out of bounds: " + base);
}
diff --git a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java
index 39e92061620..dee5c8e3a73 100644
--- a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java
@@ -424,7 +424,7 @@ public final class ByteBuffersDataInput extends DataInput
}
public ByteBuffersDataInput slice(long offset, long length) {
- if (offset < 0 || length < 0 || offset + length > this.length) {
+ if ((length | offset) < 0 || length > this.length - offset) {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
diff --git a/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
index c9c92db91b4..b05652789cf 100644
--- a/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
@@ -139,7 +139,7 @@ public class NIOFSDirectory extends FSDirectory {
@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
- if (offset < 0 || length < 0 || offset + length > this.length()) {
+ if ((length | offset) < 0 || length > this.length() - offset) {
throw new IllegalArgumentException(
"slice() "
+ sliceDescription
diff --git a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java
index 74594be5ec9..800a66a2167 100644
--- a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java
+++ b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java
@@ -597,7 +597,7 @@ abstract class MemorySegmentIndexInput extends IndexInput
*/
@Override
public final MemorySegmentIndexInput slice(String sliceDescription, long offset, long length) {
- if (offset < 0 || length < 0 || offset + length > this.length) {
+ if ((length | offset) < 0 || length > this.length - offset) {
throw new IllegalArgumentException(
"slice() "
+ sliceDescription
diff --git a/lucene/misc/src/java/org/apache/lucene/misc/store/RAFDirectory.java b/lucene/misc/src/java/org/apache/lucene/misc/store/RAFDirectory.java
index 420d6d40d6d..cd90db9abbe 100644
--- a/lucene/misc/src/java/org/apache/lucene/misc/store/RAFDirectory.java
+++ b/lucene/misc/src/java/org/apache/lucene/misc/store/RAFDirectory.java
@@ -136,7 +136,7 @@ public class RAFDirectory extends FSDirectory {
@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
- if (offset < 0 || length < 0 || offset + length > this.length()) {
+ if ((length | offset) < 0 || length > this.length() - offset) {
throw new IllegalArgumentException(
"slice() " + sliceDescription + " out of bounds: " + this);
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java
index 6defa5eb8c7..88223a4abeb 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java
@@ -771,6 +771,12 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {
slice.slice("slice3sub", 1, len / 2);
});
+ expectThrows(
+ IllegalArgumentException.class,
+ () -> {
+ i.slice("slice4", Long.MAX_VALUE - 1, 10);
+ });
+
i.close();
}
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java
index 1b4234c3d79..4d3c233257c 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java
@@ -194,7 +194,7 @@ public class SerialIOCountingDirectory extends FilterDirectory {
public IndexInput slice(
String sliceDescription, long offset, long length, ReadAdvice readAdvice)
throws IOException {
- if (offset < 0 || offset + length > sliceLength) {
+ if ((length | offset) < 0 || length > sliceLength - offset) {
throw new IllegalArgumentException();
}
IndexInput clone = in.clone();