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

tabish121 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git

commit 7804938fb2d300261dd3f1fe760ec6cdf23d7d4e
Author: Thomas Hayen <[email protected]>
AuthorDate: Tue Oct 6 08:11:13 2026 +0000

    PROTON-2982 fix index computation in ProtonCompositeBufferImpl
---
 .../buffer/impl/ProtonCompositeBufferImpl.java     |  2 +-
 .../buffer/impl/ProtonCompositeBufferImplTest.java | 26 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git 
a/protonj2/src/main/java/org/apache/qpid/protonj2/buffer/impl/ProtonCompositeBufferImpl.java
 
b/protonj2/src/main/java/org/apache/qpid/protonj2/buffer/impl/ProtonCompositeBufferImpl.java
index 4d53878a..b573f7ca 100644
--- 
a/protonj2/src/main/java/org/apache/qpid/protonj2/buffer/impl/ProtonCompositeBufferImpl.java
+++ 
b/protonj2/src/main/java/org/apache/qpid/protonj2/buffer/impl/ProtonCompositeBufferImpl.java
@@ -623,7 +623,7 @@ public final class ProtonCompositeBufferImpl extends 
SharedResource<ProtonBuffer
         if (buffers.length != 0 && length > 0) {
             final int startingPoint = findChunkWithIndex(index);
 
-            int remaining = length - (buffers[startingPoint].capacity() - 
index);
+            int remaining = length - (buffers[startingPoint].capacity() - 
(index - startIndices[startingPoint]));
             int requiredCopies = 1;
 
             // Compute how large the copied buffers array needs to be then do 
the actual copy
diff --git 
a/protonj2/src/test/java/org/apache/qpid/protonj2/buffer/impl/ProtonCompositeBufferImplTest.java
 
b/protonj2/src/test/java/org/apache/qpid/protonj2/buffer/impl/ProtonCompositeBufferImplTest.java
index e1dbe043..a5c6284e 100644
--- 
a/protonj2/src/test/java/org/apache/qpid/protonj2/buffer/impl/ProtonCompositeBufferImplTest.java
+++ 
b/protonj2/src/test/java/org/apache/qpid/protonj2/buffer/impl/ProtonCompositeBufferImplTest.java
@@ -1814,6 +1814,32 @@ public class ProtonCompositeBufferImplTest extends 
ProtonAbstractBufferTest {
         }
     }
 
+    @Test
+    public void 
testCopyRangeContainedWithinNonFirstChunkDoesNotThrowIndexOutOfBounds() {
+        try (ProtonBufferAllocator allocator = createProtonDefaultAllocator();
+             ProtonCompositeBuffer composite = allocator.composite()) {
+
+            final byte[] chunk0 = new byte[20];
+            final byte[] chunk1 = new byte[10];
+            final byte[] chunk2 = new byte[10];
+
+            for (int i = 0; i < chunk1.length; ++i) {
+                chunk1[i] = (byte) (100 + i);
+            }
+
+            composite.append(allocator.copy(chunk0));
+            composite.append(allocator.copy(chunk1));
+            composite.append(allocator.copy(chunk2));
+
+            try (ProtonBuffer copy = composite.copy(22, 3, true)) {
+                assertEquals(3, copy.capacity());
+                assertEquals((byte) 102, copy.getByte(0));
+                assertEquals((byte) 103, copy.getByte(1));
+                assertEquals((byte) 104, copy.getByte(2));
+            }
+        }
+    }
+
     @Test
     public void testSplitBufferLastBufferNoReadableBytes() {
         try (ProtonBufferAllocator allocator = createProtonDefaultAllocator()) 
{


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

Reply via email to