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 f7eb0c1b fix: validate list-view size buffers (#1003)
f7eb0c1b is described below

commit f7eb0c1b946f9d590b014c6f6f636984f4eda88e
Author: Efe <[email protected]>
AuthorDate: Mon Nov 9 06:05:49 2026 +0300

    fix: validate list-view size buffers (#1003)
    
    Minimal validation only checks the first two buffers, so it misses the
    sizes buffer in ListView and LargeListView arrays. Include that
    fixed-size third buffer to reject missing or truncated sizes before full
    validation reads them, and infer its size when unknown. Regression tests
    cover both types, sliced arrays, and empty arrays. R array-print
    snapshots also normalise version-dependent separator spacing from
    `str()`.
    
    Debug and ASan/UBSan CTest builds each passed 298 tests, with 3
    thread-safety tests skipped by the build configuration. The sanitizer
    run used `ASAN_OPTIONS=allocator_may_return_null=1:detect_leaks=0` for
    tests that deliberately request oversized allocations. clang-format
    16.0.6, codespell 2.2.5 and `git diff --check` passed. The R-devel check
    passed on GitHub with 1,762 expectations and no failures or warnings.
    
    Codex generated the fix and regression tests and ran the local checks.
---
 src/nanoarrow/common/array.c       |  7 +--
 src/nanoarrow/common/array_test.cc | 94 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+), 3 deletions(-)

diff --git a/src/nanoarrow/common/array.c b/src/nanoarrow/common/array.c
index bd02cd5e..fa657f82 100644
--- a/src/nanoarrow/common/array.c
+++ b/src/nanoarrow/common/array.c
@@ -1616,9 +1616,10 @@ static int ArrowArrayViewValidateMinimal(struct 
ArrowArrayView* array_view,
   // unknown, assign the buffer size; otherwise, validate it.
   int64_t offset_plus_length = array_view->offset + array_view->length;
 
-  // Only loop over the first two buffers because the size of the third buffer
-  // is always data dependent for all current Arrow types.
-  for (int i = 0; i < 2; i++) {
+  // List views also have a fixed-size buffer containing element sizes.
+  const int n_buffers =
+      array_view->layout.buffer_type[2] == NANOARROW_BUFFER_TYPE_SIZE ? 3 : 2;
+  for (int i = 0; i < n_buffers; i++) {
     // Ensure that the buffer sizes calculated below fit within an int64 (the 
largest
     // is the element size in bits times offset + length + 1, rounded up to 
bytes)
     int64_t element_size_bits = array_view->layout.element_size_bits[i];
diff --git a/src/nanoarrow/common/array_test.cc 
b/src/nanoarrow/common/array_test.cc
index aa340323..90b56139 100644
--- a/src/nanoarrow/common/array_test.cc
+++ b/src/nanoarrow/common/array_test.cc
@@ -3800,6 +3800,100 @@ TEST(ArrayTest, ArrayViewTestListView) {
   ArrowArrayViewReset(&array_view);
 }
 
+class ListViewSizeBufferTest : public ::testing::TestWithParam<enum ArrowType> 
{
+ protected:
+  void SetUp() override {
+    ArrowArrayViewInitFromType(array_view.get(), GetParam());
+    ASSERT_EQ(ArrowArrayViewAllocateChildren(array_view.get(), 1), 
NANOARROW_OK);
+    ArrowArrayViewInitFromType(array_view->children[0], NANOARROW_TYPE_NA);
+    element_size = array_view->layout.element_size_bits[2] / 8;
+    array_view->buffer_views[1].data.data = offsets;
+    array_view->buffer_views[1].size_bytes = sizeof(offsets);
+    array_view->buffer_views[2].data.data = sizes;
+  }
+
+  nanoarrow::UniqueArrayView array_view;
+  int64_t element_size;
+  int64_t offsets[2] = {0, 0};
+  int64_t sizes[2] = {0, 0};
+};
+
+TEST_P(ListViewSizeBufferTest, RejectsMissingSizes) {
+  struct ArrowArray child = {};
+  struct ArrowArray* children[] = {&child};
+  const void* buffers[] = {nullptr, offsets, nullptr};
+  struct ArrowArray array = {};
+  array.length = 1;
+  array.n_buffers = 3;
+  array.buffers = buffers;
+  array.n_children = 1;
+  array.children = children;
+  struct ArrowError error;
+
+  EXPECT_EQ(ArrowArrayViewSetArray(array_view.get(), &array, &error), EINVAL);
+}
+
+TEST_P(ListViewSizeBufferTest, RejectsShortSizes) {
+  struct ArrowError error;
+  array_view->length = 1;
+  for (int64_t offset : {0, 1}) {
+    array_view->offset = offset;
+    const int64_t required_size = (offset + array_view->length) * element_size;
+    for (int64_t size = 0; size < required_size; size++) {
+      array_view->buffer_views[2].size_bytes = size;
+      for (auto level :
+           {NANOARROW_VALIDATION_LEVEL_MINIMAL, 
NANOARROW_VALIDATION_LEVEL_DEFAULT,
+            NANOARROW_VALIDATION_LEVEL_FULL}) {
+        ASSERT_EQ(ArrowArrayViewValidate(array_view.get(), level, &error), 
EINVAL)
+            << "offset: " << offset << ", size: " << size << ", level: " << 
level;
+      }
+    }
+  }
+}
+
+TEST_P(ListViewSizeBufferTest, InfersUnknownSizes) {
+  struct ArrowError error;
+  array_view->length = 1;
+  array_view->offset = 1;
+  for (auto level :
+       {NANOARROW_VALIDATION_LEVEL_MINIMAL, NANOARROW_VALIDATION_LEVEL_DEFAULT,
+        NANOARROW_VALIDATION_LEVEL_FULL}) {
+    array_view->buffer_views[2].size_bytes = -1;
+    ASSERT_EQ(ArrowArrayViewValidate(array_view.get(), level, &error), 
NANOARROW_OK)
+        << error.message;
+    EXPECT_EQ(array_view->buffer_views[2].size_bytes, 2 * element_size);
+  }
+}
+
+TEST_P(ListViewSizeBufferTest, AcceptsSufficientSizes) {
+  struct ArrowError error;
+  array_view->length = 1;
+  for (int64_t offset : {0, 1}) {
+    array_view->offset = offset;
+    array_view->buffer_views[2].size_bytes = (offset + 1) * element_size;
+    EXPECT_EQ(
+        ArrowArrayViewValidate(array_view.get(), 
NANOARROW_VALIDATION_LEVEL_FULL, &error),
+        NANOARROW_OK)
+        << error.message;
+  }
+}
+
+TEST_P(ListViewSizeBufferTest, AcceptsEmptySizes) {
+  struct ArrowError error;
+  array_view->buffer_views[1].data.data = nullptr;
+  array_view->buffer_views[1].size_bytes = 0;
+  array_view->buffer_views[2].data.data = nullptr;
+  array_view->buffer_views[2].size_bytes = 0;
+  EXPECT_EQ(
+      ArrowArrayViewValidate(array_view.get(), 
NANOARROW_VALIDATION_LEVEL_FULL, &error),
+      NANOARROW_OK)
+      << error.message;
+}
+
+INSTANTIATE_TEST_SUITE_P(ArrayTest, ListViewSizeBufferTest,
+                         ::testing::Values(NANOARROW_TYPE_LIST_VIEW,
+                                           NANOARROW_TYPE_LARGE_LIST_VIEW));
+
 TEST(ArrayTest, ArrayViewTestListViewGet) {
   struct ArrowArrayView array_view;
   ArrowArrayViewInitFromType(&array_view, NANOARROW_TYPE_LIST_VIEW);

Reply via email to