You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Arawoof06 (via GitHub)" <gi...@apache.org> on 2026/10/16 08:44:07 UTC

[PR] MINOR: Bounds check delta dictionary data copy in VectorAppender [arrow-java]

Arawoof06 opened a new pull request, #1364:
URL: https://github.com/apache/arrow-java/pull/1364

   ## What's Changed
   
   `VectorAppender.visit(BaseVariableWidthVector)` and `visit(BaseLargeVariableWidthVector)` copy the delta's value bytes with `MemoryUtil.copyMemory`, starting at the delta's first offset for `lastOffset - firstOffset` bytes, and nothing checks that range against the delta data buffer. `ArrowReader.loadDictionary` passes IPC delta dictionary batches straight to `VectorBatchAppender.batchAppend`, so a stream whose VarChar delta dictionary has offsets `[0, 4, 4096]` over an 8-byte data buffer appends a 4092-byte dictionary value filled with adjacent native memory, with bounds checking enabled. The fix uses `ArrowBuf.setBytes(index, src, srcIndex, length)`, which always validates the source range, and the new test in `TestArrowReaderWriter` builds that stream and expects `IndexOutOfBoundsException`.
   
   The other copies in `VectorAppender` are already covered, because `VectorLoader` resizes the delta's validity, offset and view buffers to the row count and the end-offset read is bounds checked.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Bounds check delta dictionary data copy in VectorAppender [arrow-java]

Posted by "Arawoof06 (via GitHub)" <gi...@apache.org>.
Arawoof06 commented on code in PR #1364:
URL: https://github.com/apache/arrow-java/pull/1364#discussion_r4304112151


##########
vector/src/main/java/org/apache/arrow/vector/util/VectorAppender.java:
##########
@@ -153,10 +153,9 @@ public ValueVector visit(BaseVariableWidthVector deltaVector, Void value) {
         targetVector.getValidityBuffer());
 
     // append data buffer
-    MemoryUtil.copyMemory(
-        deltaVector.getDataBuffer().memoryAddress() + deltaDataStart,
-        targetVector.getDataBuffer().memoryAddress() + targetDataSize,
-        deltaDataSize);
+    targetVector

Review Comment:
   Agreed. Added a `validateDelta` pass at the top of both visits that runs before any realloc or `concatBits`, so a rejected delta now leaves the target as it was. The test checks that too (see below).



##########
vector/src/main/java/org/apache/arrow/vector/util/VectorAppender.java:
##########
@@ -153,10 +153,9 @@ public ValueVector visit(BaseVariableWidthVector deltaVector, Void value) {
         targetVector.getValidityBuffer());
 
     // append data buffer
-    MemoryUtil.copyMemory(
-        deltaVector.getDataBuffer().memoryAddress() + deltaDataStart,
-        targetVector.getDataBuffer().memoryAddress() + targetDataSize,
-        deltaDataSize);
+    targetVector

Review Comment:
   Yes, worth it. `validateDelta` now checks the validity buffer against `getValidityBufferSizeFromCount(valueCount)` and the offset buffer against `(valueCount + 1) * OFFSET_WIDTH` before anything reads them, so `concatBits`, the offset `copyMemory` and the rebasing loop only run on buffers that cover the value count.



##########
vector/src/main/java/org/apache/arrow/vector/util/VectorAppender.java:
##########
@@ -153,10 +153,9 @@ public ValueVector visit(BaseVariableWidthVector deltaVector, Void value) {
         targetVector.getValidityBuffer());
 
     // append data buffer
-    MemoryUtil.copyMemory(
-        deltaVector.getDataBuffer().memoryAddress() + deltaDataStart,
-        targetVector.getDataBuffer().memoryAddress() + targetDataSize,
-        deltaDataSize);
+    targetVector
+        .getDataBuffer()
+        .setBytes(targetDataSize, deltaVector.getDataBuffer(), deltaDataStart, deltaDataSize);

Review Comment:
   Done. It walks the offsets once and requires `0 <= first`, non-decreasing offsets throughout, and last `<=` data buffer capacity, so intermediate offsets are covered too. `targetDataSize + deltaDataSize` uses `Math.addExact` now.



##########
vector/src/main/java/org/apache/arrow/vector/util/VectorAppender.java:
##########
@@ -229,10 +228,9 @@ public ValueVector visit(BaseLargeVariableWidthVector deltaVector, Void value) {
         targetVector.getValidityBuffer());
 
     // append data buffer
-    MemoryUtil.copyMemory(
-        deltaVector.getDataBuffer().memoryAddress() + deltaDataStart,
-        targetVector.getDataBuffer().memoryAddress() + targetDataSize,
-        deltaDataSize);
+    targetVector

Review Comment:
   Same changes on the large path, with long offsets and `Math.addExact(long, long)`.



##########
vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java:
##########
@@ -649,6 +649,66 @@ public void testDeltaDictionary() throws Exception {
     AutoCloseables.close(closeableList);
   }
 
+  @Test
+  public void testDeltaDictionaryOffsetsExceedDataBuffer() throws Exception {
+    try (VarCharVector dictVector = newVarCharVector("dict", allocator)) {
+      setVector(
+          dictVector,
+          "foo".getBytes(StandardCharsets.UTF_8),
+          "bar".getBytes(StandardCharsets.UTF_8));
+      Dictionary dictionary = new Dictionary(dictVector, new DictionaryEncoding(1L, false, null));
+      DictionaryProvider.MapDictionaryProvider provider =
+          new DictionaryProvider.MapDictionaryProvider();
+      provider.put(dictionary);
+      Field encodedField =
+          new Field(
+              "f",
+              new FieldType(true, new ArrowType.Int(32, true), dictionary.getEncoding()),
+              null);
+      Schema schema =
+          new Schema(
+              Collections.singletonList(
+                  DictionaryUtility.toMessageFormat(encodedField, provider, new HashSet<>())));
+
+      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+      WriteChannel out = new WriteChannel(newChannel(outStream));
+      MessageSerializer.serialize(out, schema);
+
+      List<AutoCloseable> closeableList = new ArrayList<>();
+      serializeDictionaryBatch(out, dictionary, false, closeableList);
+
+      // delta dictionary whose offsets point far past its 8-byte data buffer
+      ArrowBuf validity = allocator.buffer(0);
+      ArrowBuf offsets = allocator.buffer(12);
+      offsets.setInt(0, 0);
+      offsets.setInt(4, 4);
+      offsets.setInt(8, 4096);
+      offsets.writerIndex(12);
+      ArrowBuf data = allocator.buffer(8);
+      data.setBytes(0, "bazzquxx".getBytes(StandardCharsets.UTF_8));
+      data.writerIndex(8);
+      ArrowRecordBatch deltaBatch =
+          new ArrowRecordBatch(
+              2,
+              Collections.singletonList(new ArrowFieldNode(2, 0)),
+              Arrays.asList(validity, offsets, data));
+      ArrowDictionaryBatch deltaDictionaryBatch = new ArrowDictionaryBatch(1L, deltaBatch, true);
+      MessageSerializer.serialize(out, deltaDictionaryBatch);
+      closeableList.add(deltaDictionaryBatch);
+      AutoCloseables.close(validity, offsets, data);
+
+      // write eos
+      out.writeIntLittleEndian(0);
+
+      try (ArrowStreamReader reader =
+          new ArrowStreamReader(
+              new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) {

Review Comment:
   The new checks are explicit capacity/offset comparisons rather than relying on `ArrowBuf` bounds checking, so they apply with `arrow.enable_unsafe_memory_access=true` as well, and I left the test unconditional instead of adding an `assumeTrue`. After the failure it now asserts the dictionary still has 2 values, `foo` and `bar`, and that validity bits 2 and 3 are unset. With only the earlier `setBytes` change, that validity assertion fails.



##########
vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java:
##########
@@ -649,6 +649,66 @@ public void testDeltaDictionary() throws Exception {
     AutoCloseables.close(closeableList);
   }
 
+  @Test
+  public void testDeltaDictionaryOffsetsExceedDataBuffer() throws Exception {
+    try (VarCharVector dictVector = newVarCharVector("dict", allocator)) {
+      setVector(
+          dictVector,
+          "foo".getBytes(StandardCharsets.UTF_8),
+          "bar".getBytes(StandardCharsets.UTF_8));
+      Dictionary dictionary = new Dictionary(dictVector, new DictionaryEncoding(1L, false, null));
+      DictionaryProvider.MapDictionaryProvider provider =
+          new DictionaryProvider.MapDictionaryProvider();
+      provider.put(dictionary);
+      Field encodedField =
+          new Field(
+              "f",
+              new FieldType(true, new ArrowType.Int(32, true), dictionary.getEncoding()),
+              null);
+      Schema schema =
+          new Schema(
+              Collections.singletonList(
+                  DictionaryUtility.toMessageFormat(encodedField, provider, new HashSet<>())));
+
+      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+      WriteChannel out = new WriteChannel(newChannel(outStream));
+      MessageSerializer.serialize(out, schema);
+
+      List<AutoCloseable> closeableList = new ArrayList<>();
+      serializeDictionaryBatch(out, dictionary, false, closeableList);
+
+      // delta dictionary whose offsets point far past its 8-byte data buffer
+      ArrowBuf validity = allocator.buffer(0);
+      ArrowBuf offsets = allocator.buffer(12);
+      offsets.setInt(0, 0);
+      offsets.setInt(4, 4);
+      offsets.setInt(8, 4096);
+      offsets.writerIndex(12);
+      ArrowBuf data = allocator.buffer(8);
+      data.setBytes(0, "bazzquxx".getBytes(StandardCharsets.UTF_8));
+      data.writerIndex(8);
+      ArrowRecordBatch deltaBatch =
+          new ArrowRecordBatch(
+              2,
+              Collections.singletonList(new ArrowFieldNode(2, 0)),
+              Arrays.asList(validity, offsets, data));
+      ArrowDictionaryBatch deltaDictionaryBatch = new ArrowDictionaryBatch(1L, deltaBatch, true);
+      MessageSerializer.serialize(out, deltaDictionaryBatch);
+      closeableList.add(deltaDictionaryBatch);
+      AutoCloseables.close(validity, offsets, data);
+
+      // write eos
+      out.writeIntLittleEndian(0);
+
+      try (ArrowStreamReader reader =
+          new ArrowStreamReader(
+              new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) {

Review Comment:
   Added. The malformed case is parameterized over VarChar and LargeVarChar and over four offset shapes: past the data buffer, decreasing intermediate offset, last before first, and negative start. There is also a positive test for both types with offsets `{3, 6, 9}` over `xxxbazqux`, which expects `foo, bar, baz, qux`.



##########
vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java:
##########
@@ -649,6 +649,66 @@ public void testDeltaDictionary() throws Exception {
     AutoCloseables.close(closeableList);
   }
 
+  @Test
+  public void testDeltaDictionaryOffsetsExceedDataBuffer() throws Exception {
+    try (VarCharVector dictVector = newVarCharVector("dict", allocator)) {
+      setVector(
+          dictVector,
+          "foo".getBytes(StandardCharsets.UTF_8),
+          "bar".getBytes(StandardCharsets.UTF_8));
+      Dictionary dictionary = new Dictionary(dictVector, new DictionaryEncoding(1L, false, null));
+      DictionaryProvider.MapDictionaryProvider provider =
+          new DictionaryProvider.MapDictionaryProvider();
+      provider.put(dictionary);
+      Field encodedField =
+          new Field(
+              "f",
+              new FieldType(true, new ArrowType.Int(32, true), dictionary.getEncoding()),
+              null);
+      Schema schema =
+          new Schema(
+              Collections.singletonList(
+                  DictionaryUtility.toMessageFormat(encodedField, provider, new HashSet<>())));
+
+      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+      WriteChannel out = new WriteChannel(newChannel(outStream));
+      MessageSerializer.serialize(out, schema);
+
+      List<AutoCloseable> closeableList = new ArrayList<>();
+      serializeDictionaryBatch(out, dictionary, false, closeableList);
+
+      // delta dictionary whose offsets point far past its 8-byte data buffer
+      ArrowBuf validity = allocator.buffer(0);
+      ArrowBuf offsets = allocator.buffer(12);
+      offsets.setInt(0, 0);
+      offsets.setInt(4, 4);
+      offsets.setInt(8, 4096);
+      offsets.writerIndex(12);
+      ArrowBuf data = allocator.buffer(8);
+      data.setBytes(0, "bazzquxx".getBytes(StandardCharsets.UTF_8));
+      data.writerIndex(8);
+      ArrowRecordBatch deltaBatch =
+          new ArrowRecordBatch(
+              2,
+              Collections.singletonList(new ArrowFieldNode(2, 0)),
+              Arrays.asList(validity, offsets, data));
+      ArrowDictionaryBatch deltaDictionaryBatch = new ArrowDictionaryBatch(1L, deltaBatch, true);
+      MessageSerializer.serialize(out, deltaDictionaryBatch);
+      closeableList.add(deltaDictionaryBatch);
+      AutoCloseables.close(validity, offsets, data);
+
+      // write eos
+      out.writeIntLittleEndian(0);
+
+      try (ArrowStreamReader reader =
+          new ArrowStreamReader(
+              new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) {
+        assertThrows(IndexOutOfBoundsException.class, reader::loadNextBatch);
+      }
+      AutoCloseables.close(closeableList);

Review Comment:
   Fixed. The stream setup moved into a helper that closes everything in a `finally`, and each test only holds the reader in try-with-resources.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Bounds check delta dictionary data copy in VectorAppender [arrow-java]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #1364:
URL: https://github.com/apache/arrow-java/pull/1364#issuecomment-6212440583

   
   <!-- label_helper_comment -->
   Thank you for opening a pull request!
   
   Please label the PR with one or more of:
   
   - bug-fix
   - chore
   - dependencies
   - documentation
   - enhancement
   
   Also, add the 'breaking-change' label if appropriate.
   
   See [CONTRIBUTING.md](https://github.com/apache/arrow-java/blob/main/CONTRIBUTING.md) for details.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Bounds check delta dictionary data copy in VectorAppender [arrow-java]

Posted by "jbonofre (via GitHub) via github" <gi...@arrow.apache.org>.
jbonofre merged PR #1364:
URL: https://github.com/apache/arrow-java/pull/1364


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Bounds check delta dictionary data copy in VectorAppender [arrow-java]

Posted by "jbonofre (via GitHub)" <gi...@apache.org>.
jbonofre commented on code in PR #1364:
URL: https://github.com/apache/arrow-java/pull/1364#discussion_r4295213565


##########
vector/src/main/java/org/apache/arrow/vector/util/VectorAppender.java:
##########
@@ -153,10 +153,9 @@ public ValueVector visit(BaseVariableWidthVector deltaVector, Void value) {
         targetVector.getValidityBuffer());
 
     // append data buffer
-    MemoryUtil.copyMemory(
-        deltaVector.getDataBuffer().memoryAddress() + deltaDataStart,
-        targetVector.getDataBuffer().memoryAddress() + targetDataSize,
-        deltaDataSize);
+    targetVector

Review Comment:
   Nice improvement, this turns a raw out-of-bounds native copy into a checked one. One concern is ordering. By the time this `setBytes` runs, the target's data buffer has been reallocated for `deltaDataSize` and `concatBits` has already written the delta's validity bits into the target.
   
   If the check throws, the target is left with extra validity bits while `valueCount`, offsets and `lastSet` are unchanged.
   
   Could we validate the delta (last offset against its data buffer capacity) before any realloc or write, so a rejected delta leaves the target untouched?



##########
vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java:
##########
@@ -649,6 +649,66 @@ public void testDeltaDictionary() throws Exception {
     AutoCloseables.close(closeableList);
   }
 
+  @Test
+  public void testDeltaDictionaryOffsetsExceedDataBuffer() throws Exception {
+    try (VarCharVector dictVector = newVarCharVector("dict", allocator)) {
+      setVector(
+          dictVector,
+          "foo".getBytes(StandardCharsets.UTF_8),
+          "bar".getBytes(StandardCharsets.UTF_8));
+      Dictionary dictionary = new Dictionary(dictVector, new DictionaryEncoding(1L, false, null));
+      DictionaryProvider.MapDictionaryProvider provider =
+          new DictionaryProvider.MapDictionaryProvider();
+      provider.put(dictionary);
+      Field encodedField =
+          new Field(
+              "f",
+              new FieldType(true, new ArrowType.Int(32, true), dictionary.getEncoding()),
+              null);
+      Schema schema =
+          new Schema(
+              Collections.singletonList(
+                  DictionaryUtility.toMessageFormat(encodedField, provider, new HashSet<>())));
+
+      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+      WriteChannel out = new WriteChannel(newChannel(outStream));
+      MessageSerializer.serialize(out, schema);
+
+      List<AutoCloseable> closeableList = new ArrayList<>();
+      serializeDictionaryBatch(out, dictionary, false, closeableList);
+
+      // delta dictionary whose offsets point far past its 8-byte data buffer
+      ArrowBuf validity = allocator.buffer(0);
+      ArrowBuf offsets = allocator.buffer(12);
+      offsets.setInt(0, 0);
+      offsets.setInt(4, 4);
+      offsets.setInt(8, 4096);
+      offsets.writerIndex(12);
+      ArrowBuf data = allocator.buffer(8);
+      data.setBytes(0, "bazzquxx".getBytes(StandardCharsets.UTF_8));
+      data.writerIndex(8);
+      ArrowRecordBatch deltaBatch =
+          new ArrowRecordBatch(
+              2,
+              Collections.singletonList(new ArrowFieldNode(2, 0)),
+              Arrays.asList(validity, offsets, data));
+      ArrowDictionaryBatch deltaDictionaryBatch = new ArrowDictionaryBatch(1L, deltaBatch, true);
+      MessageSerializer.serialize(out, deltaDictionaryBatch);
+      closeableList.add(deltaDictionaryBatch);
+      AutoCloseables.close(validity, offsets, data);
+
+      // write eos
+      out.writeIntLittleEndian(0);
+
+      try (ArrowStreamReader reader =
+          new ArrowStreamReader(
+              new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) {
+        assertThrows(IndexOutOfBoundsException.class, reader::loadNextBatch);
+      }
+      AutoCloseables.close(closeableList);

Review Comment:
   If `assertThrows` fails, `closeableList` is never closed and the allocator leaks. Consider wrapping it in a `finally` so the original assertion failure isn't masked by a leak error.



##########
vector/src/main/java/org/apache/arrow/vector/util/VectorAppender.java:
##########
@@ -229,10 +228,9 @@ public ValueVector visit(BaseLargeVariableWidthVector deltaVector, Void value) {
         targetVector.getValidityBuffer());
 
     // append data buffer
-    MemoryUtil.copyMemory(
-        deltaVector.getDataBuffer().memoryAddress() + deltaDataStart,
-        targetVector.getDataBuffer().memoryAddress() + targetDataSize,
-        deltaDataSize);
+    targetVector

Review Comment:
   The same comments applies here 😄 



##########
vector/src/main/java/org/apache/arrow/vector/util/VectorAppender.java:
##########
@@ -153,10 +153,9 @@ public ValueVector visit(BaseVariableWidthVector deltaVector, Void value) {
         targetVector.getValidityBuffer());
 
     // append data buffer
-    MemoryUtil.copyMemory(
-        deltaVector.getDataBuffer().memoryAddress() + deltaDataStart,
-        targetVector.getDataBuffer().memoryAddress() + targetDataSize,
-        deltaDataSize);
+    targetVector
+        .getDataBuffer()
+        .setBytes(targetDataSize, deltaVector.getDataBuffer(), deltaDataStart, deltaDataSize);

Review Comment:
   `deltaDataSize` is derived from the delta's offsets without validation. A last offset smaller than the first gives a negative size, and a very large one can overflow the int when added to `targetDataSize`.
   Intermediate offsets are not checked against the data size either, so later reads of the appender vector could go out of bounds even when the final offset passes.
   
   It would be good to guard `0 <= start <= end <= dataBuffer.capacity()` and use `Math.addExact` or similar.



##########
vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java:
##########
@@ -649,6 +649,66 @@ public void testDeltaDictionary() throws Exception {
     AutoCloseables.close(closeableList);
   }
 
+  @Test
+  public void testDeltaDictionaryOffsetsExceedDataBuffer() throws Exception {
+    try (VarCharVector dictVector = newVarCharVector("dict", allocator)) {
+      setVector(
+          dictVector,
+          "foo".getBytes(StandardCharsets.UTF_8),
+          "bar".getBytes(StandardCharsets.UTF_8));
+      Dictionary dictionary = new Dictionary(dictVector, new DictionaryEncoding(1L, false, null));
+      DictionaryProvider.MapDictionaryProvider provider =
+          new DictionaryProvider.MapDictionaryProvider();
+      provider.put(dictionary);
+      Field encodedField =
+          new Field(
+              "f",
+              new FieldType(true, new ArrowType.Int(32, true), dictionary.getEncoding()),
+              null);
+      Schema schema =
+          new Schema(
+              Collections.singletonList(
+                  DictionaryUtility.toMessageFormat(encodedField, provider, new HashSet<>())));
+
+      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+      WriteChannel out = new WriteChannel(newChannel(outStream));
+      MessageSerializer.serialize(out, schema);
+
+      List<AutoCloseable> closeableList = new ArrayList<>();
+      serializeDictionaryBatch(out, dictionary, false, closeableList);
+
+      // delta dictionary whose offsets point far past its 8-byte data buffer
+      ArrowBuf validity = allocator.buffer(0);
+      ArrowBuf offsets = allocator.buffer(12);
+      offsets.setInt(0, 0);
+      offsets.setInt(4, 4);
+      offsets.setInt(8, 4096);
+      offsets.writerIndex(12);
+      ArrowBuf data = allocator.buffer(8);
+      data.setBytes(0, "bazzquxx".getBytes(StandardCharsets.UTF_8));
+      data.writerIndex(8);
+      ArrowRecordBatch deltaBatch =
+          new ArrowRecordBatch(
+              2,
+              Collections.singletonList(new ArrowFieldNode(2, 0)),
+              Arrays.asList(validity, offsets, data));
+      ArrowDictionaryBatch deltaDictionaryBatch = new ArrowDictionaryBatch(1L, deltaBatch, true);
+      MessageSerializer.serialize(out, deltaDictionaryBatch);
+      closeableList.add(deltaDictionaryBatch);
+      AutoCloseables.close(validity, offsets, data);
+
+      // write eos
+      out.writeIntLittleEndian(0);
+
+      try (ArrowStreamReader reader =
+          new ArrowStreamReader(
+              new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) {

Review Comment:
   This only covers the `VarChar` path.
   
   Could we add a `LargeVarChar` case for the `BaseLargeVariableWidthVector` visit? A positive case with a valid delta whose data start offset is non-zero would also help, since that exercises the `deltaDataStart` argument to `setBytes`.



##########
vector/src/test/java/org/apache/arrow/vector/ipc/TestArrowReaderWriter.java:
##########
@@ -649,6 +649,66 @@ public void testDeltaDictionary() throws Exception {
     AutoCloseables.close(closeableList);
   }
 
+  @Test
+  public void testDeltaDictionaryOffsetsExceedDataBuffer() throws Exception {
+    try (VarCharVector dictVector = newVarCharVector("dict", allocator)) {
+      setVector(
+          dictVector,
+          "foo".getBytes(StandardCharsets.UTF_8),
+          "bar".getBytes(StandardCharsets.UTF_8));
+      Dictionary dictionary = new Dictionary(dictVector, new DictionaryEncoding(1L, false, null));
+      DictionaryProvider.MapDictionaryProvider provider =
+          new DictionaryProvider.MapDictionaryProvider();
+      provider.put(dictionary);
+      Field encodedField =
+          new Field(
+              "f",
+              new FieldType(true, new ArrowType.Int(32, true), dictionary.getEncoding()),
+              null);
+      Schema schema =
+          new Schema(
+              Collections.singletonList(
+                  DictionaryUtility.toMessageFormat(encodedField, provider, new HashSet<>())));
+
+      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+      WriteChannel out = new WriteChannel(newChannel(outStream));
+      MessageSerializer.serialize(out, schema);
+
+      List<AutoCloseable> closeableList = new ArrayList<>();
+      serializeDictionaryBatch(out, dictionary, false, closeableList);
+
+      // delta dictionary whose offsets point far past its 8-byte data buffer
+      ArrowBuf validity = allocator.buffer(0);
+      ArrowBuf offsets = allocator.buffer(12);
+      offsets.setInt(0, 0);
+      offsets.setInt(4, 4);
+      offsets.setInt(8, 4096);
+      offsets.writerIndex(12);
+      ArrowBuf data = allocator.buffer(8);
+      data.setBytes(0, "bazzquxx".getBytes(StandardCharsets.UTF_8));
+      data.writerIndex(8);
+      ArrowRecordBatch deltaBatch =
+          new ArrowRecordBatch(
+              2,
+              Collections.singletonList(new ArrowFieldNode(2, 0)),
+              Arrays.asList(validity, offsets, data));
+      ArrowDictionaryBatch deltaDictionaryBatch = new ArrowDictionaryBatch(1L, deltaBatch, true);
+      MessageSerializer.serialize(out, deltaDictionaryBatch);
+      closeableList.add(deltaDictionaryBatch);
+      AutoCloseables.close(validity, offsets, data);
+
+      // write eos
+      out.writeIntLittleEndian(0);
+
+      try (ArrowStreamReader reader =
+          new ArrowStreamReader(
+              new ByteArrayReadableSeekableByteChannel(outStream.toByteArray()), allocator)) {

Review Comment:
   `setBytes` bounds checking is gated by `BoundsChecking.BOUNDS_CHECKING_ENABLED`. With `arrow.enable_unsafe_memory_access=true` the check is skipped, no exception is thrown, and this test fails (and the fix does nothing in that mode). Consider skipping the test with an `assumeTrue` when bounds checking is off. Also, asserting only the exception type lets the partial-mutation issue go unnoticed. After the failure, please assert that the dictionary vector still has its original two values.



##########
vector/src/main/java/org/apache/arrow/vector/util/VectorAppender.java:
##########
@@ -153,10 +153,9 @@ public ValueVector visit(BaseVariableWidthVector deltaVector, Void value) {
         targetVector.getValidityBuffer());
 
     // append data buffer
-    MemoryUtil.copyMemory(
-        deltaVector.getDataBuffer().memoryAddress() + deltaDataStart,
-        targetVector.getDataBuffer().memoryAddress() + targetDataSize,
-        deltaDataSize);
+    targetVector

Review Comment:
   Also, only the data copy is checked now. The validity copy via `concatBits`, the offset buffer `MemoryUtil.copyMemory`, and the offset-rebasing loop still read the delta without bounds checks. A delta whose offset buffer is shorter than `(valueCount + 1) * 4` bytes, or whose validity buffer is shorter than `valueCount` bits, would still read out-of-bounds native memory.
   
   Is it worth validating those buffer sizes up front too?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org