From e236ee620fb9b7c6b2e104eca1979c32f625c88d Mon Sep 17 00:00:00 2001 From: abdul rawoof Date: Thu, 26 Nov 2026 15:47:40 +0530 Subject: [PATCH] MINOR: Reject negative first offsets in ValidateVectorDataVisitor --- .../validate/ValidateVectorDataVisitor.java | 6 +++ .../validate/TestValidateVectorFull.java | 48 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/vector/src/main/java/org/apache/arrow/vector/validate/ValidateVectorDataVisitor.java b/vector/src/main/java/org/apache/arrow/vector/validate/ValidateVectorDataVisitor.java index 355229d744..0d650d7371 100644 --- a/vector/src/main/java/org/apache/arrow/vector/validate/ValidateVectorDataVisitor.java +++ b/vector/src/main/java/org/apache/arrow/vector/validate/ValidateVectorDataVisitor.java @@ -51,6 +51,8 @@ private void validateOffsetBuffer(ValueVector vector, int valueCount) { // verify that the values in the offset buffer is non-decreasing int prevValue = offsetBuffer.getInt(0); + validateOrThrow( + prevValue >= 0, "The value at position 0 of the offset buffer is negative: %s.", prevValue); for (int i = 1; i <= valueCount; i++) { int curValue = offsetBuffer.getInt(i * 4); validateOrThrow( @@ -77,6 +79,10 @@ private void validateLargeOffsetBuffer(ValueVector vector, int valueCount) { // verify that the values in the large offset buffer is non-decreasing long prevValue = offsetBuffer.getLong(0); + validateOrThrow( + prevValue >= 0L, + "The value at position 0 of the large offset buffer is negative: %s.", + prevValue); for (int i = 1; i <= valueCount; i++) { long curValue = offsetBuffer.getLong((long) i * 8); validateOrThrow( 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 6ad56f3946..e4aefbfe50 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 @@ -32,8 +32,10 @@ import org.apache.arrow.vector.DecimalVector; import org.apache.arrow.vector.Float4Vector; import org.apache.arrow.vector.IntVector; +import org.apache.arrow.vector.LargeVarBinaryVector; import org.apache.arrow.vector.LargeVarCharVector; import org.apache.arrow.vector.ValueVector; +import org.apache.arrow.vector.VarBinaryVector; import org.apache.arrow.vector.VarCharVector; import org.apache.arrow.vector.ViewVarCharVector; import org.apache.arrow.vector.complex.DenseUnionVector; @@ -463,4 +465,50 @@ public void testValidateDecimal256() { assertTrue(e.getMessage().contains("Decimal")); } } + + @Test + public void testValidateNegativeFirstOffset() { + byte[] aaa = "aaa".getBytes(StandardCharsets.UTF_8); + byte[] bbb = "bbb".getBytes(StandardCharsets.UTF_8); + + try (final VarBinaryVector vector = new VarBinaryVector("v", allocator)) { + setVector(vector, aaa, bbb); + vector.getOffsetBuffer().setInt(0, -4); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); + assertTrue( + e.getMessage().contains("The value at position 0 of the offset buffer is negative")); + } + + try (final LargeVarBinaryVector vector = new LargeVarBinaryVector("v", allocator)) { + vector.setSafe(0, aaa); + vector.setSafe(1, bbb); + vector.setValueCount(2); + vector.getOffsetBuffer().setLong(0, -4); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); + assertTrue( + e.getMessage() + .contains("The value at position 0 of the large offset buffer is negative")); + } + + try (final ListVector vector = ListVector.empty("v", allocator)) { + setVector(vector, Arrays.asList(1, 2, 3), Arrays.asList(4, 5)); + vector.getOffsetBuffer().setInt(0, -1); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); + assertTrue( + e.getMessage().contains("The value at position 0 of the offset buffer is negative")); + } + + try (final LargeListVector vector = LargeListVector.empty("v", allocator)) { + setVector(vector, Arrays.asList(1, 2, 3), Arrays.asList(4, 5)); + vector.getOffsetBuffer().setLong(0, -1); + ValidateUtil.ValidateException e = + assertThrows(ValidateUtil.ValidateException.class, () -> validateFull(vector)); + assertTrue( + e.getMessage() + .contains("The value at position 0 of the large offset buffer is negative")); + } + } }