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

dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new cfba36c87988 [SPARK-57428][CORE] Improve `read` bounds checks with 
`Objects.checkFromIndexSize`
cfba36c87988 is described below

commit cfba36c8798821432cafee16a0464f6797ca9a90
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Sun Jun 14 08:46:03 2026 -0700

    [SPARK-57428][CORE] Improve `read` bounds checks with 
`Objects.checkFromIndexSize`
    
    ### What changes were proposed in this pull request?
    
    This PR replaces the manual offset/length bounds checks in the `read(byte[] 
b, int offset, int len)` methods of `ReadAheadInputStream` and 
`NioBufferedFileInputStream` with `java.util.Objects.checkFromIndexSize(offset, 
len, b.length)`.
    
    ### Why are the changes needed?
    
    `Objects.checkFromIndexSize` (available since Java 9) does the same 
overflow-safe validation and throws the same `IndexOutOfBoundsException`, so 
the manual checks are redundant.
    
    Additionally, this provides a better exception message than the current 
one, `new IndexOutOfBoundsException()`, in case of failure.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    Pass the CIs.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Claude Opus 4.8)
    
    Closes #56490 from dongjoon-hyun/SPARK-57428.
    
    Authored-by: Dongjoon Hyun <[email protected]>
    Signed-off-by: Dongjoon Hyun <[email protected]>
    (cherry picked from commit 40bebb968d224ebae0e53733ce17077a9ff265c1)
    Signed-off-by: Dongjoon Hyun <[email protected]>
---
 .../main/java/org/apache/spark/io/NioBufferedFileInputStream.java    | 5 ++---
 core/src/main/java/org/apache/spark/io/ReadAheadInputStream.java     | 5 ++---
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git 
a/core/src/main/java/org/apache/spark/io/NioBufferedFileInputStream.java 
b/core/src/main/java/org/apache/spark/io/NioBufferedFileInputStream.java
index 412d612c7f1d..a67a8bb266c1 100644
--- a/core/src/main/java/org/apache/spark/io/NioBufferedFileInputStream.java
+++ b/core/src/main/java/org/apache/spark/io/NioBufferedFileInputStream.java
@@ -27,6 +27,7 @@ import java.lang.ref.Cleaner;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.file.StandardOpenOption;
+import java.util.Objects;
 
 /**
  * {@link InputStream} implementation which uses direct buffer
@@ -87,9 +88,7 @@ public final class NioBufferedFileInputStream extends 
InputStream {
 
   @Override
   public synchronized int read(byte[] b, int offset, int len) throws 
IOException {
-    if (offset < 0 || len < 0 || offset + len < 0 || offset + len > b.length) {
-      throw new IndexOutOfBoundsException();
-    }
+    Objects.checkFromIndexSize(offset, len, b.length);
     if (!refill()) {
       return -1;
     }
diff --git a/core/src/main/java/org/apache/spark/io/ReadAheadInputStream.java 
b/core/src/main/java/org/apache/spark/io/ReadAheadInputStream.java
index 91ee2d8ccbe9..0a1f51154a6d 100644
--- a/core/src/main/java/org/apache/spark/io/ReadAheadInputStream.java
+++ b/core/src/main/java/org/apache/spark/io/ReadAheadInputStream.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InterruptedIOException;
 import java.nio.ByteBuffer;
+import java.util.Objects;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -260,9 +261,7 @@ public class ReadAheadInputStream extends InputStream {
 
   @Override
   public int read(byte[] b, int offset, int len) throws IOException {
-    if (offset < 0 || len < 0 || len > b.length - offset) {
-      throw new IndexOutOfBoundsException();
-    }
+    Objects.checkFromIndexSize(offset, len, b.length);
     if (len == 0) {
       return 0;
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to