This is an automated email from the ASF dual-hosted git repository.
lidavidm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git
The following commit(s) were added to refs/heads/main by this push:
new 91e39e143c fix(c/driver/postgresql): handle overflow/invalid array
dimensions (#5051)
91e39e143c is described below
commit 91e39e143c53961bcbabb0f8eea859602728e703
Author: David Li <[email protected]>
AuthorDate: Wed Nov 18 17:21:40 2026 -0800
fix(c/driver/postgresql): handle overflow/invalid array dimensions (#5051)
Also not something super important, but could in theory happen with an
untrusted server or proxy.
Assisted-by: DeepSeek V4.1 Flash
Assisted-by: GPT-6.2 Sol
---
.../postgresql/copy/postgres_copy_reader_test.cc | 71 ++++++++++++++++++++++
c/driver/postgresql/copy/reader.h | 13 +++-
2 files changed, 81 insertions(+), 3 deletions(-)
diff --git a/c/driver/postgresql/copy/postgres_copy_reader_test.cc
b/c/driver/postgresql/copy/postgres_copy_reader_test.cc
index aa60660c4f..2b244f376d 100644
--- a/c/driver/postgresql/copy/postgres_copy_reader_test.cc
+++ b/c/driver/postgresql/copy/postgres_copy_reader_test.cc
@@ -841,6 +841,77 @@ TEST(PostgresCopyUtilsTest, PostgresCopyReadArray) {
ASSERT_EQ(data_buffer[4], 123);
}
+TEST(PostgresCopyUtilsTest, PostgresCopyReadArrayDimensionOverflow) {
+ // An int4 array with three dimensions of INT32_MAX elements each.
+ const uint8_t buffer[] = {
+ 0x50, 0x47, 0x43, 0x4f, 0x50, 0x59, 0x0a, 0xff, 0x0d, 0x0a, 0x00, 0x00,
0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24,
0x00,
+ 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x7f,
0xff,
+ 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x7f, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00,
+ 0x01, 0x7f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff};
+ ArrowBufferView data;
+ data.data.as_uint8 = buffer;
+ data.size_bytes = sizeof(buffer);
+
+ PostgresType input_type(PostgresTypeId::kRecord);
+ input_type.AppendChild("col", PostgresType(PostgresTypeId::kInt4).Array());
+
+ PostgresCopyStreamTester tester;
+ ArrowError error = {};
+ ASSERT_EQ(tester.Init(input_type, &error), NANOARROW_OK) << error.message;
+ ASSERT_EQ(tester.ReadAll(&data, &error), EINVAL) << error.message;
+ ASSERT_STREQ(error.message, "Array element count overflow");
+}
+
+TEST(PostgresCopyUtilsTest, PostgresCopyReadArrayEmpty) {
+ const uint8_t buffer[] = {0x50, 0x47, 0x43, 0x4f, 0x50, 0x59, 0x0a, 0xff,
0x0d, 0x0a,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0xff,
0xff};
+ ArrowBufferView data;
+ data.data.as_uint8 = buffer;
+ data.size_bytes = sizeof(buffer);
+
+ PostgresType input_type(PostgresTypeId::kRecord);
+ input_type.AppendChild("col", PostgresType(PostgresTypeId::kInt4).Array());
+
+ PostgresCopyStreamTester tester;
+ ArrowError error = {};
+ ASSERT_EQ(tester.Init(input_type, &error), NANOARROW_OK) << error.message;
+ ASSERT_EQ(tester.ReadAll(&data, &error), ENODATA) << error.message;
+ ASSERT_EQ(data.size_bytes, 0);
+
+ nanoarrow::UniqueArray array;
+ ASSERT_EQ(tester.GetArray(array.get(), &error), NANOARROW_OK) <<
error.message;
+ ASSERT_EQ(array->length, 1);
+ const auto* list = array->children[0];
+ ASSERT_EQ(list->null_count, 0);
+ ASSERT_EQ(list->children[0]->length, 0);
+ const auto* offsets = static_cast<const int32_t*>(list->buffers[1]);
+ ASSERT_EQ(offsets[0], 0);
+ ASSERT_EQ(offsets[1], 0);
+}
+
+TEST(PostgresCopyUtilsTest, PostgresCopyReadArrayNegativeDimensionLength) {
+ const uint8_t buffer[] = {0x50, 0x47, 0x43, 0x4f, 0x50, 0x59, 0x0a, 0xff,
0x0d, 0x0a,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
0x01, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0xff,
0xff, 0xff,
+ 0xff, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff};
+ ArrowBufferView data;
+ data.data.as_uint8 = buffer;
+ data.size_bytes = sizeof(buffer);
+
+ PostgresType input_type(PostgresTypeId::kRecord);
+ input_type.AppendChild("col", PostgresType(PostgresTypeId::kInt4).Array());
+
+ PostgresCopyStreamTester tester;
+ ArrowError error = {};
+ ASSERT_EQ(tester.Init(input_type, &error), NANOARROW_OK) << error.message;
+ ASSERT_EQ(tester.ReadAll(&data, &error), EINVAL) << error.message;
+ ASSERT_STREQ(error.message, "Negative array dimension length: -1");
+}
+
TEST(PostgresCopyUtilsTest, PostgresCopyReadInt2vector) {
ArrowBufferView data;
data.data.as_uint8 = kTestPgCopyInt2vector;
diff --git a/c/driver/postgresql/copy/reader.h
b/c/driver/postgresql/copy/reader.h
index 82ed7e05f4..e922505f13 100644
--- a/c/driver/postgresql/copy/reader.h
+++ b/c/driver/postgresql/copy/reader.h
@@ -630,12 +630,12 @@ class PostgresCopyArrayFieldReader : public
PostgresCopyFieldReader {
// (e.g. testing) since the OID can be specific to each database
if (n_dim < 0) [[unlikely]] {
- ArrowErrorSet(error, "Expected array n_dim > 0 but got %d",
+ ArrowErrorSet(error, "Expected array n_dim >= 0 but got %d",
static_cast<int>(n_dim)); // NOLINT(runtime/int)
return EINVAL;
}
- // This is apparently allowed
+ // PostgreSQL sends ordinary empty arrays with zero dimensions.
if (n_dim == 0) {
NANOARROW_RETURN_NOT_OK(ArrowArrayFinishElement(array));
return NANOARROW_OK;
@@ -645,7 +645,14 @@ class PostgresCopyArrayFieldReader : public
PostgresCopyFieldReader {
for (int32_t i = 0; i < n_dim; i++) {
int32_t dim_size;
NANOARROW_RETURN_NOT_OK(ReadChecked<int32_t>(data, &dim_size, error));
- n_items *= dim_size;
+ if (dim_size < 0) [[unlikely]] {
+ ArrowErrorSet(error, "Negative array dimension length: %" PRId32,
dim_size);
+ return EINVAL;
+ }
+ if (!psnip_safe_int64_mul(&n_items, n_items, dim_size)) [[unlikely]] {
+ ArrowErrorSet(error, "Array element count overflow");
+ return EINVAL;
+ }
int32_t lower_bound;
NANOARROW_RETURN_NOT_OK(ReadChecked<int32_t>(data, &lower_bound, error));