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

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 547cd99fa MINOR: Check buffer bounds in BaseFixedWidthVector.copyFrom 
(#1505)
547cd99fa is described below

commit 547cd99fa7f9e2394294003aa2c06c4128597ed3
Author: Abdul Rawoof Khan <[email protected]>
AuthorDate: Thu Nov 19 15:44:08 2026 +0530

    MINOR: Check buffer bounds in BaseFixedWidthVector.copyFrom (#1505)
    
    ## What's Changed
    
    `BaseFixedWidthVector.copyFrom` copied `typeWidth` bytes with a raw
    `MemoryUtil.copyMemory`, so the source range was never checked against
    the source data buffer. `loadFieldBuffers` does not compare the data
    buffer length with the field node length, and full validation is off by
    default in `ArrowReader`, so an IPC batch or dictionary that declares 16
    Int values with a 4-byte data buffer loads without error. `get(8)` on
    that vector throws `IndexOutOfBoundsException`, but
    `copyFrom`/`copyFromSafe` (also reached through
    `TransferPair.copyValueSafe`, e.g. from `DictionaryEncoder.decode` and
    the struct/list/union `copyFrom` paths) copies whatever native memory
    follows the buffer into the target vector, with bounds checking enabled.
    
    The copy now goes through `ArrowBuf.setBytes(index, src, srcIndex,
    length)`, which always checks the source range and checks the
    destination when bounds checking is on, matching the variable-width
    `copyFrom`, which already uses the checked `getBytes`. The validity bit
    is set after the copy, so a rejected copy leaves the target slot null.
    
    The new test in `TestCopyFrom` loads an `IntVector` with a short data
    buffer and expects `IndexOutOfBoundsException` from both `copyFrom` and
    `copyFromSafe`. Before the change, the same setup copied the
    `0x41414141` written just past the slice into the target. The `vector`
    and `algorithm` test suites pass.
---
 .../apache/arrow/vector/BaseFixedWidthVector.java  | 11 ++++++-----
 .../java/org/apache/arrow/vector/TestCopyFrom.java | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git 
a/vector/src/main/java/org/apache/arrow/vector/BaseFixedWidthVector.java 
b/vector/src/main/java/org/apache/arrow/vector/BaseFixedWidthVector.java
index ff0d5a586..5b21abbf2 100644
--- a/vector/src/main/java/org/apache/arrow/vector/BaseFixedWidthVector.java
+++ b/vector/src/main/java/org/apache/arrow/vector/BaseFixedWidthVector.java
@@ -26,7 +26,6 @@ import org.apache.arrow.memory.ArrowBuf;
 import org.apache.arrow.memory.BufferAllocator;
 import org.apache.arrow.memory.util.ArrowBufPointer;
 import org.apache.arrow.memory.util.ByteFunctionHelpers;
-import org.apache.arrow.memory.util.MemoryUtil;
 import org.apache.arrow.memory.util.hash.ArrowBufHasher;
 import org.apache.arrow.util.Preconditions;
 import org.apache.arrow.vector.compare.VectorVisitor;
@@ -832,11 +831,13 @@ public abstract class BaseFixedWidthVector extends 
BaseValueVector
     if (from.isNull(fromIndex)) {
       BitVectorHelper.unsetBit(this.getValidityBuffer(), thisIndex);
     } else {
+      this.getDataBuffer()
+          .setBytes(
+              (long) thisIndex * typeWidth,
+              from.getDataBuffer(),
+              (long) fromIndex * typeWidth,
+              typeWidth);
       BitVectorHelper.setBit(this.getValidityBuffer(), thisIndex);
-      MemoryUtil.copyMemory(
-          from.getDataBuffer().memoryAddress() + (long) fromIndex * typeWidth,
-          this.getDataBuffer().memoryAddress() + (long) thisIndex * typeWidth,
-          typeWidth);
     }
   }
 
diff --git a/vector/src/test/java/org/apache/arrow/vector/TestCopyFrom.java 
b/vector/src/test/java/org/apache/arrow/vector/TestCopyFrom.java
index bd0b44a14..b7c375b03 100644
--- a/vector/src/test/java/org/apache/arrow/vector/TestCopyFrom.java
+++ b/vector/src/test/java/org/apache/arrow/vector/TestCopyFrom.java
@@ -20,15 +20,19 @@ import static org.apache.arrow.vector.TestUtils.newVector;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.math.BigDecimal;
 import java.nio.charset.StandardCharsets;
 import java.time.Duration;
 import java.time.Period;
+import java.util.Arrays;
 import java.util.Objects;
+import org.apache.arrow.memory.ArrowBuf;
 import org.apache.arrow.memory.BufferAllocator;
 import org.apache.arrow.memory.RootAllocator;
+import org.apache.arrow.vector.ipc.message.ArrowFieldNode;
 import org.apache.arrow.vector.types.Types.MinorType;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
@@ -1098,4 +1102,22 @@ public class TestCopyFrom {
       assertEquals("1234567890", 
Objects.requireNonNull(vc2.getObject(5)).toString());
     }
   }
+
+  @Test
+  public void testCopyFromChecksSourceDataBuffer() {
+    try (ArrowBuf data = allocator.buffer(64);
+        IntVector source = new IntVector("source", allocator);
+        IntVector target = new IntVector("target", allocator)) {
+      data.setZero(0, data.capacity());
+      // The batch declares 16 non-null values but its data buffer only holds 
1 of them
+      source.loadFieldBuffers(
+          new ArrowFieldNode(16, 0), Arrays.asList(allocator.getEmpty(), 
data.slice(0, 4)));
+      target.allocateNew(16);
+
+      assertThrows(IndexOutOfBoundsException.class, () -> source.get(8));
+      assertThrows(IndexOutOfBoundsException.class, () -> target.copyFrom(8, 
0, source));
+      assertThrows(IndexOutOfBoundsException.class, () -> 
target.copyFromSafe(8, 0, source));
+      assertTrue(target.isNull(0));
+    }
+  }
 }

Reply via email to