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 10c55b9c8 MINOR: Reject truncated UTF-8 sequences in Text.validateUTF8 
(#1522)
10c55b9c8 is described below

commit 10c55b9c8a01cf5db557effce133eaf9818c51b2
Author: Abdul Rawoof Khan <[email protected]>
AuthorDate: Wed Nov 25 23:39:21 2026 +0530

    MINOR: Reject truncated UTF-8 sequences in Text.validateUTF8 (#1522)
    
    ## What's Changed
    
    `Text.validateUTF8Internal` walks the bytes with a lead-byte/trail-byte
    state machine, but when the loop runs out of input it returns success
    even if it is still waiting for trail bytes. A value that ends partway
    through a multi-byte sequence, such as `C3`, `E2 82` or `F0 9F 98`, is
    reported as valid UTF-8.
    
    That helper backs `validateScalars` in `VarCharVector`,
    `LargeVarCharVector` and `ViewVarCharVector`, so
    `ValueVectorUtility.validateFull` (and `ArrowReader` with
    `setValidateBatches(true)`, the option recommended for untrusted IPC
    input) accepts a Utf8 column holding invalid UTF-8. Lead bytes that are
    invalid on their own (`FF FE`) were already rejected, which is why the
    existing tests pass.
    
    The fix reports the end position as malformed when the input ends
    outside the lead-byte state. Valid input is unaffected.
    `testValidateTruncatedUTF8` in `TestValidateVectorFull` covers truncated
    2, 3 and 4 byte sequences for VarChar and LargeVarChar. It fails without
    the change and passes with it, and the rest of the `vector` tests still
    pass.
---
 .../java/org/apache/arrow/vector/util/Text.java    |  4 ++++
 .../vector/validate/TestValidateVectorFull.java    | 22 ++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/vector/src/main/java/org/apache/arrow/vector/util/Text.java 
b/vector/src/main/java/org/apache/arrow/vector/util/Text.java
index 2edb00408..9dd07d5b4 100644
--- a/vector/src/main/java/org/apache/arrow/vector/util/Text.java
+++ b/vector/src/main/java/org/apache/arrow/vector/util/Text.java
@@ -504,6 +504,10 @@ public class Text extends ReusableByteArray {
       } // switch (state)
       count++;
     }
+    if (state != LEAD_BYTE) {
+      // the input ended in the middle of a multi-byte sequence
+      return Optional.of(count);
+    }
     return Optional.empty();
   }
 
diff --git 
a/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorFull.java
 
b/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorFull.java
index cb7645330..6ad56f394 100644
--- 
a/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorFull.java
+++ 
b/vector/src/test/java/org/apache/arrow/vector/validate/TestValidateVectorFull.java
@@ -408,6 +408,28 @@ public class TestValidateVectorFull {
     }
   }
 
+  @Test
+  public void testValidateTruncatedUTF8() {
+    // multi-byte sequences cut short at the end of the value
+    byte[][] truncated = {
+      {(byte) 0xC3}, {(byte) 0xE2, (byte) 0x82}, {'a', (byte) 0xF0, (byte) 
0x9F, (byte) 0x98}
+    };
+    for (byte[] value : truncated) {
+      try (final VarCharVector vector = new VarCharVector("v", allocator)) {
+        setVector(vector, "aaa".getBytes(StandardCharsets.UTF_8), value);
+        ValidateUtil.ValidateException e =
+            assertThrows(ValidateUtil.ValidateException.class, 
vector::validateFull);
+        assertTrue(e.getMessage().contains("UTF"));
+      }
+      try (final LargeVarCharVector vector = new LargeVarCharVector("v", 
allocator)) {
+        setVector(vector, "aaa".getBytes(StandardCharsets.UTF_8), value);
+        ValidateUtil.ValidateException e =
+            assertThrows(ValidateUtil.ValidateException.class, 
vector::validateFull);
+        assertTrue(e.getMessage().contains("UTF"));
+      }
+    }
+  }
+
   @Test
   public void testValidateDecimal() {
     try (final DecimalVector vector =

Reply via email to