You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fory.apache.org by ch...@apache.org on 2026/10/07 01:56:58 UTC

(fory) branch main updated: fix(java): advance readerIndex in continueReadBinarySize (#4133)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new bb2c1c9a0 fix(java): advance readerIndex in continueReadBinarySize (#4133)
bb2c1c9a0 is described below

commit bb2c1c9a0e2cf5b4f8c0152325814c357d9c7105
Author: Hkwaynewing <Ri...@live.cn>
AuthorDate: Wed Oct 7 12:56:52 2026 +1100

    fix(java): advance readerIndex in continueReadBinarySize (#4133)
    
    ## Why?
    The 3/4-byte varint length-prefix fast path of readBinarySize()
    delegates to continueReadBinarySize(), which decoded the size but never
    assigned readerIndex, unlike the <=2-byte path. readBytesAndSize() (and
    readStringAndSize / *WithSize primitive readers) then copied the body
    from the stale index, i.e. starting at the first prefix byte: payloads
    with big enough length came back shifted by the prefix width, silently
    corrupted, or failed downstream with "reserved header bitmap flags"
    errors.
    
    ## What does this PR do?
    
    Fix both the base and the java25 MemoryBuffer by assigning readerIndex =
    readIdx in continueReadBinarySize, matching continueReadVarUInt32. Add a
    regression test covering 3-byte and 4-byte prefixes plus a trailing read
    to verify index alignment.
    
    The Android path (MemoryOps.readBinarySize -> readVarUInt32) already
    advances readerIndex correctly and needed no change.
    
    ## Related issues
    
    No related issue found
    
    Co-authored-by: Wayne Wen <ww...@seek.com.au>
---
 .../java/org/apache/fory/memory/MemoryBuffer.java     |  1 +
 .../java25/org/apache/fory/memory/MemoryBuffer.java   |  1 +
 .../java/org/apache/fory/memory/MemoryBufferTest.java | 19 +++++++++++++++++++
 3 files changed, 21 insertions(+)

diff --git a/java/fory-core/src/main/java/org/apache/fory/memory/MemoryBuffer.java b/java/fory-core/src/main/java/org/apache/fory/memory/MemoryBuffer.java
index 526000930..d9f141294 100644
--- a/java/fory-core/src/main/java/org/apache/fory/memory/MemoryBuffer.java
+++ b/java/fory-core/src/main/java/org/apache/fory/memory/MemoryBuffer.java
@@ -3907,6 +3907,7 @@ public final class MemoryBuffer {
     if (diff < binarySize) {
       streamReader.fillBuffer(binarySize - diff);
     }
+    readerIndex = readIdx;
     return binarySize;
   }
 
diff --git a/java/fory-core/src/main/java25/org/apache/fory/memory/MemoryBuffer.java b/java/fory-core/src/main/java25/org/apache/fory/memory/MemoryBuffer.java
index 2e3a834b7..14944c2f4 100644
--- a/java/fory-core/src/main/java25/org/apache/fory/memory/MemoryBuffer.java
+++ b/java/fory-core/src/main/java25/org/apache/fory/memory/MemoryBuffer.java
@@ -3298,6 +3298,7 @@ public final class MemoryBuffer {
     if (diff < binarySize) {
       streamReader.fillBuffer(binarySize - diff);
     }
+    readerIndex = readIdx;
     return binarySize;
   }
 
diff --git a/java/fory-core/src/test/java/org/apache/fory/memory/MemoryBufferTest.java b/java/fory-core/src/test/java/org/apache/fory/memory/MemoryBufferTest.java
index 99fe14686..5961309e5 100644
--- a/java/fory-core/src/test/java/org/apache/fory/memory/MemoryBufferTest.java
+++ b/java/fory-core/src/test/java/org/apache/fory/memory/MemoryBufferTest.java
@@ -789,6 +789,25 @@ public class MemoryBufferTest {
     assertEquals(chars, readChars);
   }
 
+  @Test
+  public void testReadBytesAndSizeLargePrefix() {
+    // Lengths that need 3-byte (>= 2^14) and 4-byte (>= 2^21) varint prefixes. The 3/4-byte
+    // prefix path must advance readerIndex past the prefix, otherwise readBytesAndSize() returns
+    // the prefix bytes shifted into the payload body.
+    Random random = new Random(42);
+    for (int len : new int[] {16_384, 16_385, 25_000, 2_097_152, 2_097_153}) {
+      MemoryBuffer buf = MemoryUtils.buffer(len + 64);
+      byte[] expected = new byte[len];
+      random.nextBytes(expected);
+      buf.writeBytesWithSize(expected);
+      // Trailing payload: verifies the reader index is aligned for subsequent reads.
+      buf.writeInt32(0x12345678);
+      byte[] actual = buf.readBytesAndSize();
+      assertEquals(expected, actual);
+      assertEquals(0x12345678, buf.readInt32());
+    }
+  }
+
   @Test
   public void testReadCharsAndSizeAlignment() {
     MemoryBuffer buf = MemoryUtils.buffer(8);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org