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 8d9407cee MINOR: Check buffer bounds before writing long decimal 
values (#1444)
8d9407cee is described below

commit 8d9407cee5ea7706185aaad7c7295ca6c7b05c38
Author: Robert Kruszewski <[email protected]>
AuthorDate: Wed Nov 11 05:52:37 2026 +0000

    MINOR: Check buffer bounds before writing long decimal values (#1444)
    
    ## What's Changed
    
    Split out from gh-1228
    
    Avoids out of bounds writes when writing long to decimal buffer,
    validate the bounds before writing to the address.
---
 .../apache/arrow/vector/util/DecimalUtility.java   |  4 +-
 .../arrow/vector/util/TestDecimalWriteBounds.java  | 50 ++++++++++++++++++++++
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git 
a/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java 
b/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
index 647e460e4..a1a9cf7e1 100644
--- a/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
+++ b/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
@@ -167,7 +167,9 @@ public class DecimalUtility {
           "DecimalUtility.writeLongToArrowBuf() currently supports "
               + "128-bit or 256-bit width data");
     }
-    final long addressOfValue = bytebuf.memoryAddress() + (long) index * 
byteWidth;
+    final long startIndex = (long) index * byteWidth;
+    bytebuf.checkBytes(startIndex, startIndex + byteWidth);
+    final long addressOfValue = bytebuf.memoryAddress() + startIndex;
     final long padValue = Long.signum(value) == -1 ? -1L : 0L;
     if (LITTLE_ENDIAN) {
       MemoryUtil.putLong(addressOfValue, value);
diff --git 
a/vector/src/test/java/org/apache/arrow/vector/util/TestDecimalWriteBounds.java 
b/vector/src/test/java/org/apache/arrow/vector/util/TestDecimalWriteBounds.java
new file mode 100644
index 000000000..8332c80e1
--- /dev/null
+++ 
b/vector/src/test/java/org/apache/arrow/vector/util/TestDecimalWriteBounds.java
@@ -0,0 +1,50 @@
+/*
+ * 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.arrow.vector.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.arrow.memory.ArrowBuf;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+public class TestDecimalWriteBounds {
+  @ParameterizedTest
+  @ValueSource(ints = {16, 32})
+  public void testWriteLongChecksBufferBounds(int byteWidth) {
+    try (BufferAllocator allocator = new RootAllocator(128);
+        ArrowBuf buffer = allocator.buffer(3L * byteWidth)) {
+      for (long i = 0; i < buffer.capacity(); i++) {
+        buffer.setByte(i, 100);
+      }
+      // The surrounding allocation keeps this regression test safe even if 
bounds checks fail.
+      ArrowBuf value = buffer.slice(byteWidth, byteWidth);
+      assertThrows(
+          IndexOutOfBoundsException.class,
+          () -> DecimalUtility.writeLongToArrowBuf(1, value, -1, byteWidth));
+      assertThrows(
+          IndexOutOfBoundsException.class,
+          () -> DecimalUtility.writeLongToArrowBuf(1, value, 1, byteWidth));
+      for (long i = 0; i < buffer.capacity(); i++) {
+        assertEquals((byte) 100, buffer.getByte(i));
+      }
+    }
+  }
+}

Reply via email to