This is an automated email from the ASF dual-hosted git repository.

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 26bd2a98 fix: Don't read offsets from a too-short offsets buffer in 
default validation (#987)
26bd2a98 is described below

commit 26bd2a982921729f56e2a424d75a027a97debfc5
Author: Pedro Z <[email protected]>
AuthorDate: Wed Nov 11 17:57:50 2026 +0100

    fix: Don't read offsets from a too-short offsets buffer in default 
validation (#987)
    
    `ArrowArrayViewValidateMinimal()` deliberately allows an empty offsets
    buffer for a zero-length array. Its minimum size is `(offset_plus_length
    != 0) * element_size * (offset_plus_length + 1)`, which is 0 for a
    zero-length array. So a buffer of 1 to 3 bytes (or 1 to 7 for the large
    types) passes. `ArrowArrayViewValidateDefault()` then reads offsets
    whenever `size_bytes != 0`, which reads a whole `int32_t` or `int64_t`
    past the end of the buffer.
    
    Default validation now reads the first and last offsets only when the
    buffer holds `offset_plus_length + 1` of them. This applies to string
    and binary, large string and binary, list and map, and large list. A
    buffer that is long enough behaves as before. A too-short buffer for a
    zero-length array is treated like an empty one.
    
    The new `ArrayViewTestShortOffsetsBuffer` test validates a zero-length
    string, large string, list and large list. Each has an offsets buffer
    one byte short of a single offset, in a heap allocation of exactly that
    size. Without the fix, ASan reports a heap-buffer-overflow. With it, all
    four validate.
    
    The input was found by fuzzing the IPC reader in the
    [zuarrow](https://github.com/pedrobtz/zuarrow) R package, which vendors
    nanoarrow. An IPC message can declare a 2-byte offsets buffer for a
    zero-length string column.
    
    `ctest` passes in a plain Debug build and with ASan and UBSan (410 tests
    each). The changed files are formatted with the pinned clang-format
    16.0.6.
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
    Co-authored-by: Dewey Dunnington <[email protected]>
---
 src/nanoarrow/common/array.c       | 20 ++++++++++++++++----
 src/nanoarrow/common/array_test.cc | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/src/nanoarrow/common/array.c b/src/nanoarrow/common/array.c
index fa657f82..5b5cab93 100644
--- a/src/nanoarrow/common/array.c
+++ b/src/nanoarrow/common/array.c
@@ -1897,7 +1897,10 @@ static int ArrowArrayViewValidateDefault(struct 
ArrowArrayView* array_view,
   switch (array_view->storage_type) {
     case NANOARROW_TYPE_STRING:
     case NANOARROW_TYPE_BINARY:
-      if (array_view->buffer_views[1].size_bytes != 0) {
+      // A zero-length array may have an offsets buffer that is too short to
+      // hold one offset: only read offsets from a buffer that holds them
+      if (array_view->buffer_views[1].size_bytes >=
+          (offset_plus_length + 1) * (int64_t)sizeof(int32_t)) {
         first_offset = 
ArrowBufferViewGetInt32Unsafe(&array_view->buffer_views[1],
                                                      array_view->offset);
         if (first_offset < 0) {
@@ -1935,7 +1938,10 @@ static int ArrowArrayViewValidateDefault(struct 
ArrowArrayView* array_view,
 
     case NANOARROW_TYPE_LARGE_STRING:
     case NANOARROW_TYPE_LARGE_BINARY:
-      if (array_view->buffer_views[1].size_bytes != 0) {
+      // A zero-length array may have an offsets buffer that is too short to
+      // hold one offset: only read offsets from a buffer that holds them
+      if (array_view->buffer_views[1].size_bytes >=
+          (offset_plus_length + 1) * (int64_t)sizeof(int64_t)) {
         first_offset = 
ArrowBufferViewGetInt64Unsafe(&array_view->buffer_views[1],
                                                      array_view->offset);
         if (first_offset < 0) {
@@ -1986,7 +1992,10 @@ static int ArrowArrayViewValidateDefault(struct 
ArrowArrayView* array_view,
 
     case NANOARROW_TYPE_LIST:
     case NANOARROW_TYPE_MAP:
-      if (array_view->buffer_views[1].size_bytes != 0) {
+      // A zero-length array may have an offsets buffer that is too short to
+      // hold one offset: only read offsets from a buffer that holds them
+      if (array_view->buffer_views[1].size_bytes >=
+          (offset_plus_length + 1) * (int64_t)sizeof(int32_t)) {
         first_offset = 
ArrowBufferViewGetInt32Unsafe(&array_view->buffer_views[1],
                                                      array_view->offset);
         if (first_offset < 0) {
@@ -2016,7 +2025,10 @@ static int ArrowArrayViewValidateDefault(struct 
ArrowArrayView* array_view,
       break;
 
     case NANOARROW_TYPE_LARGE_LIST:
-      if (array_view->buffer_views[1].size_bytes != 0) {
+      // A zero-length array may have an offsets buffer that is too short to
+      // hold one offset: only read offsets from a buffer that holds them
+      if (array_view->buffer_views[1].size_bytes >=
+          (offset_plus_length + 1) * (int64_t)sizeof(int64_t)) {
         first_offset = 
ArrowBufferViewGetInt64Unsafe(&array_view->buffer_views[1],
                                                      array_view->offset);
         if (first_offset < 0) {
diff --git a/src/nanoarrow/common/array_test.cc 
b/src/nanoarrow/common/array_test.cc
index 90b56139..723b5796 100644
--- a/src/nanoarrow/common/array_test.cc
+++ b/src/nanoarrow/common/array_test.cc
@@ -20,6 +20,7 @@
 #include <cmath>
 #include <cstdint>
 #include <cstring>
+#include <memory>
 #include <thread>
 
 #if defined(NANOARROW_BUILD_TESTS_WITH_ARROW)
@@ -2842,6 +2843,38 @@ TEST(ArrayTest, ArrayViewTestMove) {
   ArrowArrayViewReset(&array_view2);
 }
 
+TEST(ArrayTest, ArrayViewTestShortOffsetsBuffer) {
+  // A zero-length array passes minimal validation with an offsets buffer that 
is
+  // too short to hold one offset; default validation must not read an offset 
from it
+  struct ArrowError error;
+  for (enum ArrowType type : {NANOARROW_TYPE_STRING, 
NANOARROW_TYPE_LARGE_STRING,
+                              NANOARROW_TYPE_LIST, NANOARROW_TYPE_LARGE_LIST}) 
{
+    nanoarrow::UniqueSchema schema;
+    ASSERT_EQ(ArrowSchemaInitFromType(schema.get(), type), NANOARROW_OK);
+    if (type == NANOARROW_TYPE_LIST || type == NANOARROW_TYPE_LARGE_LIST) {
+      ASSERT_EQ(ArrowSchemaSetType(schema->children[0], NANOARROW_TYPE_INT32),
+                NANOARROW_OK);
+    }
+
+    nanoarrow::UniqueArrayView array_view;
+    ASSERT_EQ(ArrowArrayViewInitFromSchema(array_view.get(), schema.get(), 
&error),
+              NANOARROW_OK);
+    ArrowArrayViewSetLength(array_view.get(), 0);
+
+    // One byte short of a single offset, in a heap buffer of exactly that size
+    int64_t offset_bytes =
+        ArrowArrayViewGetBufferElementSizeBits(array_view.get(), 1) / 8;
+    std::unique_ptr<uint8_t[]> offsets(new uint8_t[offset_bytes - 1]());
+    array_view->buffer_views[1].data.as_uint8 = offsets.get();
+    array_view->buffer_views[1].size_bytes = offset_bytes - 1;
+
+    EXPECT_EQ(ArrowArrayViewValidate(array_view.get(), 
NANOARROW_VALIDATION_LEVEL_DEFAULT,
+                                     &error),
+              NANOARROW_OK)
+        << ArrowTypeString(type) << ": " << error.message;
+  }
+}
+
 TEST(ArrayTest, ArrayViewTestBufferSizeOverflow) {
   struct ArrowError error;
 

Reply via email to