Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/nanoarrow/common/array_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 22 additions & 0 deletions src/nanoarrow/common/array_stream_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>

#include <limits>

#include "nanoarrow/nanoarrow.hpp"

using testing::ElementsAre;
Expand Down Expand Up @@ -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<int64_t>::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;
Expand Down
3 changes: 2 additions & 1 deletion src/nanoarrow/nanoarrow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading