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 ff0d5a586e..5b21abbf25 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.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 void copyFrom(int fromIndex, int thisIndex, ValueVector from) { 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 bd0b44a144..b7c375b03b 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.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 void testCopySafeArrow7837() { 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)); + } + } }