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

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


The following commit(s) were added to refs/heads/main by this push:
     new bbbc8ff926 [CELEBORN-2560] Fix zero-length reads in LimitedInputStream
bbbc8ff926 is described below

commit bbbc8ff926af25f12e788b9a76b0b3896b3130ec
Author: F-yuyu <[email protected]>
AuthorDate: Wed Nov 18 15:17:00 2026 +0800

    [CELEBORN-2560] Fix zero-length reads in LimitedInputStream
    
    ### What changes were proposed in this pull request?
    
    JIRA: [CELEBORN-2560](https://issues.apache.org/jira/browse/CELEBORN-2560)
    
    Validate read arguments and return 0 for valid zero-length reads in
    LimitedInputStream before checking the remaining limit.
    
    Add regression tests for zero-length reads and invalid arguments.
    
    ### Why are the changes needed?
    
    InputStream.read(byte[], int, int) requires valid zero-length reads to
    return 0, including at EOF.
    
    LimitedInputStream currently returns -1 when its limit is exhausted.
    It also skips argument validation in that case.
    
    ### Does this PR resolve a correctness bug?
    
    - [x] Yes
    
    ### Does this PR introduce _any_ user-facing change?
    
    - [x] Yes
    Zero-length reads return 0, and invalid arguments are checked even when
    the remaining limit is zero.
    
    ### How was this patch tested?
    
    - Added LimitedInputStreamSuiteJ with 4 regression tests.
    - Ran:
      ./build/mvn -B -ntp -pl common -am test -Dtest=LimitedInputStreamSuiteJ 
-DwildcardSuites=none
    - Ran:
      ./build/mvn -B -ntp -pl common spotless:check
    - Checked the PR diff with git diff --check.
    
    Closes #3978 from F-yuyu/fix/limited-inputstream-read.
    
    Authored-by: F-yuyu <[email protected]>
    Signed-off-by: zhengtao <[email protected]>
    
    AI-Contributed/Feature: 0/4
    AI-Contributed/UT: 0/76
---
 .../common/network/util/LimitedInputStream.java    |  4 ++
 .../network/util/LimitedInputStreamSuiteJ.java     | 76 ++++++++++++++++++++++
 2 files changed, 80 insertions(+)

diff --git 
a/common/src/main/java/org/apache/celeborn/common/network/util/LimitedInputStream.java
 
b/common/src/main/java/org/apache/celeborn/common/network/util/LimitedInputStream.java
index 2f0c1c24a3..01b00723fb 100644
--- 
a/common/src/main/java/org/apache/celeborn/common/network/util/LimitedInputStream.java
+++ 
b/common/src/main/java/org/apache/celeborn/common/network/util/LimitedInputStream.java
@@ -82,6 +82,10 @@ public final class LimitedInputStream extends 
FilterInputStream {
 
   @Override
   public int read(byte[] b, int off, int len) throws IOException {
+    Preconditions.checkPositionIndexes(off, off + len, b.length);
+    if (len == 0) {
+      return 0;
+    }
     if (left == 0) {
       return -1;
     }
diff --git 
a/common/src/test/java/org/apache/celeborn/common/network/util/LimitedInputStreamSuiteJ.java
 
b/common/src/test/java/org/apache/celeborn/common/network/util/LimitedInputStreamSuiteJ.java
new file mode 100644
index 0000000000..7b17e48a72
--- /dev/null
+++ 
b/common/src/test/java/org/apache/celeborn/common/network/util/LimitedInputStreamSuiteJ.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.celeborn.common.network.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class LimitedInputStreamSuiteJ {
+
+  @Test
+  public void testZeroLengthReadAtZeroLimit() throws IOException {
+    try (LimitedInputStream stream =
+        new LimitedInputStream(new ByteArrayInputStream(new byte[] {1}), 0)) {
+      byte[] buffer = new byte[1];
+      assertEquals(0, stream.read(buffer, 0, 0));
+      assertEquals(-1, stream.read(buffer, 0, 1));
+    }
+  }
+
+  @Test
+  public void testZeroLengthReadAfterLimitReached() throws IOException {
+    ByteArrayInputStream wrapped = new ByteArrayInputStream(new byte[] {1, 2, 
3});
+    try (LimitedInputStream stream = new LimitedInputStream(wrapped, 2)) {
+      byte[] buffer = new byte[3];
+      assertEquals(0, stream.read(buffer, 0, 0));
+      assertEquals(2, stream.read(buffer, 0, buffer.length));
+      assertEquals(1, buffer[0]);
+      assertEquals(2, buffer[1]);
+      assertEquals(0, stream.read(buffer, buffer.length, 0));
+      assertEquals(-1, stream.read(buffer, 0, 1));
+      assertEquals(3, wrapped.read());
+    }
+  }
+
+  @Test
+  public void testZeroLengthReadAtUnderlyingEof() throws IOException {
+    try (LimitedInputStream stream =
+        new LimitedInputStream(new ByteArrayInputStream(new byte[0]), 1)) {
+      byte[] buffer = new byte[1];
+      assertEquals(-1, stream.read(buffer, 0, 1));
+      assertEquals(0, stream.read(buffer, 0, 0));
+    }
+  }
+
+  @Test
+  public void testInvalidReadArgumentsAtZeroLimit() throws IOException {
+    try (LimitedInputStream stream =
+        new LimitedInputStream(new ByteArrayInputStream(new byte[0]), 0)) {
+      byte[] buffer = new byte[1];
+      assertThrows(NullPointerException.class, () -> stream.read(null, 0, 0));
+      assertThrows(IndexOutOfBoundsException.class, () -> stream.read(buffer, 
-1, 0));
+      assertThrows(IndexOutOfBoundsException.class, () -> stream.read(buffer, 
0, -1));
+      assertThrows(IndexOutOfBoundsException.class, () -> stream.read(buffer, 
2, 0));
+    }
+  }
+}

Reply via email to