From 2d4dbe9c03e5ee6d071e57378954c6cf6cae408d Mon Sep 17 00:00:00 2001 From: pedrobtz Date: Thu, 5 Nov 2026 16:27:05 +0100 Subject: [PATCH] fix: Return the schema when ArrowBasicArrayStreamInit() fails to allocate arrays When ArrowMalloc() failed for the arrays, ArrowBasicArrayStreamInit() called ArrowBasicArrayStreamRelease() on a stream whose private_data had not been stored yet. The release callback then read the caller's unset release and private_data fields, and the schema already moved into the private data leaked. Move the schema back to the caller instead, as the first allocation failure leaves it, and free the private data. Co-Authored-By: Claude Opus 5.5 --- src/nanoarrow/common/array_stream.c | 4 +++- src/nanoarrow/common/array_stream_test.cc | 22 ++++++++++++++++++++++ src/nanoarrow/nanoarrow.h | 3 ++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/nanoarrow/common/array_stream.c b/src/nanoarrow/common/array_stream.c index 886ea7372..d81009d6b 100644 --- a/src/nanoarrow/common/array_stream.c +++ b/src/nanoarrow/common/array_stream.c @@ -106,7 +106,9 @@ ArrowErrorCode ArrowBasicArrayStreamInit(struct ArrowArrayStream* array_stream, private_data->arrays = (struct ArrowArray*)ArrowMalloc(n_arrays * sizeof(struct ArrowArray)); if (private_data->arrays == NULL) { - ArrowBasicArrayStreamRelease(array_stream); + // array_stream is not initialized yet, so give the schema back to the caller + // as the first allocation failure above does + ArrowSchemaMove(&private_data->schema, schema); ArrowFree(private_data); return ENOMEM; } diff --git a/src/nanoarrow/common/array_stream_test.cc b/src/nanoarrow/common/array_stream_test.cc index 25870b416..7c3426639 100644 --- a/src/nanoarrow/common/array_stream_test.cc +++ b/src/nanoarrow/common/array_stream_test.cc @@ -18,6 +18,8 @@ #include #include +#include + #include "nanoarrow/nanoarrow.hpp" using testing::ElementsAre; @@ -77,6 +79,26 @@ TEST(ArrayStreamTest, ArrayStreamTestEmpty) { ArrowArrayStreamRelease(&array_stream); } +TEST(ArrayStreamTest, ArrayStreamTestInitError) { +#if !defined(__SANITIZE_ADDRESS__) + struct ArrowArrayStream array_stream; + array_stream.release = nullptr; + struct ArrowSchema schema; + + // If the arrays can't be allocated, the caller keeps the schema and the stream is + // left uninitialized + ASSERT_EQ(ArrowSchemaInitFromType(&schema, NANOARROW_TYPE_INT32), NANOARROW_OK); + EXPECT_EQ(ArrowBasicArrayStreamInit( + &array_stream, &schema, + std::numeric_limits::max() / sizeof(struct ArrowArray)), + ENOMEM); + EXPECT_EQ(array_stream.release, nullptr); + ASSERT_NE(schema.release, nullptr); + EXPECT_STREQ(schema.format, "i"); + ArrowSchemaRelease(&schema); +#endif +} + TEST(ArrayStreamTest, ArrayStreamTestIncomplete) { struct ArrowArrayStream array_stream; struct ArrowArray array; diff --git a/src/nanoarrow/nanoarrow.h b/src/nanoarrow/nanoarrow.h index d31d67a81..e0dea0dd1 100644 --- a/src/nanoarrow/nanoarrow.h +++ b/src/nanoarrow/nanoarrow.h @@ -1383,7 +1383,8 @@ static inline void ArrowArrayViewGetDecimalUnsafe(const struct ArrowArrayView* a /// /// This function moves the ownership of schema to the array_stream. If /// this function returns NANOARROW_OK, the caller is responsible for -/// releasing the ArrowArrayStream. +/// releasing the ArrowArrayStream; otherwise, the caller retains ownership +/// of schema. NANOARROW_DLL ArrowErrorCode ArrowBasicArrayStreamInit( struct ArrowArrayStream* array_stream, struct ArrowSchema* schema, int64_t n_arrays);