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 fcad763fc8 fix(c/driver/postgresql): add more checking of inputs
(#5040)
fcad763fc8 is described below
commit fcad763fc89990337015335f27e65fd899f04c15
Author: David Li <[email protected]>
AuthorDate: Tue Nov 17 18:04:03 2026 -0800
fix(c/driver/postgresql): add more checking of inputs (#5040)
This shouldn't be strictly necessary so long as you trust the server,
but it also doesn't hurt.
Assisted-by: DeepSeek V4.1 Flash
Assisted-by: GPT-6.1 Sol
---
.../postgresql/copy/postgres_copy_reader_test.cc | 288 +++++++++++++++++++++
c/driver/postgresql/copy/reader.h | 99 ++++---
2 files changed, 350 insertions(+), 37 deletions(-)
diff --git a/c/driver/postgresql/copy/postgres_copy_reader_test.cc
b/c/driver/postgresql/copy/postgres_copy_reader_test.cc
index 6e648efc87..e4cc545563 100644
--- a/c/driver/postgresql/copy/postgres_copy_reader_test.cc
+++ b/c/driver/postgresql/copy/postgres_copy_reader_test.cc
@@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.
+#include <cstring>
+#include <memory>
#include <string>
#include <vector>
@@ -1018,4 +1020,290 @@ TEST(PostgresCopyUtilsTest, JsonbOverflow) {
// N.B. unfortunately testing with NUMERIC is difficult since we can't add all
// that much to the buffer on each iteration, so the unit test takes too long
+namespace {
+
+template <typename T>
+std::vector<uint8_t> RawBytes(T value) {
+ std::vector<uint8_t> out(sizeof(T));
+ std::memcpy(out.data(), &value, sizeof(T));
+ return out;
+}
+
+void InitReaderForTest(PostgresCopyFieldReader* reader, ArrowSchema* schema,
+ nanoarrow::UniqueArray* array) {
+ ASSERT_EQ(NANOARROW_OK, reader->InitSchema(schema));
+ ASSERT_EQ(NANOARROW_OK, ArrowArrayInitFromSchema(array->get(), schema,
nullptr));
+ ASSERT_EQ(NANOARROW_OK, ArrowArrayStartAppending(array->get()));
+ ASSERT_EQ(NANOARROW_OK, reader->InitArray(array->get()));
+}
+
+// Test a reader where the payload is shorter than expected
+void CheckTruncatedPayload(PostgresCopyFieldReader* reader, ArrowArray* array,
+ int32_t width) {
+ for (int32_t available = 0; available < width; available++) {
+ // Use padded backing storage so that the unsafe reader stays inside the
+ // allocation and the test fails deterministically instead of crashing.
+ std::vector<uint8_t> backing(static_cast<size_t>(width), 0);
+ ArrowBufferView data;
+ data.data.as_uint8 = backing.data();
+ data.size_bytes = available;
+
+ const uint8_t* input_pointer = data.data.as_uint8;
+ const int64_t input_size = data.size_bytes;
+ const int64_t output_length = array->length;
+
+ ArrowError error;
+ std::memset(&error, 0, sizeof(ArrowError));
+ ASSERT_EQ(EINVAL, reader->Read(&data, width, array, &error));
+
+ const std::string expected = "Expected " + std::to_string(width) +
+ " bytes of field data but got " +
+ std::to_string(available) + " bytes of input";
+ ASSERT_STREQ(error.message, expected.c_str());
+ ASSERT_EQ(data.data.as_uint8, input_pointer);
+ ASSERT_EQ(data.size_bytes, input_size);
+ ASSERT_EQ(array->length, output_length);
+ }
+}
+
+// Check a valid payload of all zeroes of the correct length
+void CheckZeroPayload(PostgresCopyFieldReader* reader, ArrowArray* array,
int32_t width,
+ const std::vector<uint8_t>& expected_data) {
+ std::vector<uint8_t> backing(static_cast<size_t>(width), 0);
+ ArrowBufferView data;
+ data.data.as_uint8 = backing.data();
+ data.size_bytes = width;
+
+ const uint8_t* input_pointer = data.data.as_uint8;
+ const int64_t output_index = array->length;
+
+ ArrowError error;
+ std::memset(&error, 0, sizeof(ArrowError));
+ ASSERT_EQ(NANOARROW_OK, reader->Read(&data, width, array, &error));
+ ASSERT_EQ(data.data.as_uint8, input_pointer + width);
+ ASSERT_EQ(data.size_bytes, 0);
+ ASSERT_EQ(array->length, output_index + 1);
+
+ ArrowBitmap* validity = ArrowArrayValidityBitmap(array);
+ if (validity->buffer.data != nullptr) {
+ ASSERT_TRUE(ArrowBitGet(validity->buffer.data, output_index));
+ }
+
+ ArrowBuffer* data_buffer = ArrowArrayBuffer(array, 1);
+ ASSERT_EQ(data_buffer->size_bytes,
static_cast<int64_t>(expected_data.size()));
+ ASSERT_EQ(std::memcmp(data_buffer->data, expected_data.data(),
expected_data.size()),
+ 0);
+}
+
+// Check a valid payload that should resolve to null (because size is -1 or 0)
+void CheckNullPayload(PostgresCopyFieldReader* reader, ArrowArray* array,
int32_t width,
+ int32_t field_size_bytes) {
+ std::vector<uint8_t> backing(static_cast<size_t>(width), 0);
+ ArrowBufferView data;
+ data.data.as_uint8 = backing.data();
+ data.size_bytes = width;
+
+ const uint8_t* input_pointer = data.data.as_uint8;
+ const int64_t output_index = array->length;
+
+ ArrowError error;
+ std::memset(&error, 0, sizeof(ArrowError));
+ ASSERT_EQ(NANOARROW_OK, reader->Read(&data, field_size_bytes, array,
&error));
+ ASSERT_EQ(data.data.as_uint8, input_pointer);
+ ASSERT_EQ(data.size_bytes, width);
+ ASSERT_EQ(array->length, output_index + 1);
+
+ ArrowBitmap* validity = ArrowArrayValidityBitmap(array);
+ ASSERT_NE(validity->buffer.data, nullptr);
+ ASSERT_FALSE(ArrowBitGet(validity->buffer.data, output_index));
+}
+
+// Check an invalid payload (because the field length is wrong)
+void CheckInvalidFieldSize(PostgresCopyFieldReader* reader, ArrowArray* array,
+ int32_t field_size_bytes,
+ const std::string& expected_message) {
+ std::vector<uint8_t> backing(static_cast<size_t>(field_size_bytes) + 1, 0);
+ ArrowBufferView data;
+ data.data.as_uint8 = backing.data();
+ data.size_bytes = static_cast<int64_t>(backing.size());
+
+ const uint8_t* input_pointer = data.data.as_uint8;
+ const int64_t input_size = data.size_bytes;
+ const int64_t output_length = array->length;
+
+ ArrowError error;
+ std::memset(&error, 0, sizeof(ArrowError));
+ ASSERT_EQ(EINVAL, reader->Read(&data, field_size_bytes, array, &error));
+ ASSERT_STREQ(error.message, expected_message.c_str());
+ ASSERT_EQ(data.data.as_uint8, input_pointer);
+ ASSERT_EQ(data.size_bytes, input_size);
+ ASSERT_EQ(array->length, output_length);
+}
+
+} // namespace
+
+TEST(FieldReaderInvalidInputTest, Boolean) {
+ nanoarrow::UniqueSchema schema;
+ ASSERT_EQ(NANOARROW_OK, ArrowSchemaInitFromType(schema.get(),
NANOARROW_TYPE_BOOL));
+ auto reader = std::make_unique<PostgresCopyBooleanFieldReader>();
+ nanoarrow::UniqueArray array;
+ ASSERT_NO_FATAL_FAILURE(InitReaderForTest(reader.get(), schema.get(),
&array));
+
+ CheckTruncatedPayload(reader.get(), array.get(), 1);
+ CheckZeroPayload(reader.get(), array.get(), 1, RawBytes<int8_t>(0));
+ CheckNullPayload(reader.get(), array.get(), 1, -1);
+ CheckNullPayload(reader.get(), array.get(), 1, 0);
+ CheckInvalidFieldSize(reader.get(), array.get(), 2,
+ "Expected field with one byte but found field with 2
bytes");
+ CheckInvalidFieldSize(reader.get(), array.get(), 3,
+ "Expected field with one byte but found field with 3
bytes");
+}
+
+TEST(FieldReaderInvalidInputTest, Int16) {
+ nanoarrow::UniqueSchema schema;
+ ASSERT_EQ(NANOARROW_OK, ArrowSchemaInitFromType(schema.get(),
NANOARROW_TYPE_INT16));
+ auto reader =
std::make_unique<PostgresCopyNetworkEndianFieldReader<int16_t>>();
+ nanoarrow::UniqueArray array;
+ ASSERT_NO_FATAL_FAILURE(InitReaderForTest(reader.get(), schema.get(),
&array));
+
+ CheckTruncatedPayload(reader.get(), array.get(), 2);
+ CheckZeroPayload(reader.get(), array.get(), 2, RawBytes<int16_t>(0));
+ CheckNullPayload(reader.get(), array.get(), 2, -1);
+ CheckNullPayload(reader.get(), array.get(), 2, 0);
+ CheckInvalidFieldSize(reader.get(), array.get(), 1,
+ "Expected field with 2 bytes but found field with 1
bytes");
+ CheckInvalidFieldSize(reader.get(), array.get(), 3,
+ "Expected field with 2 bytes but found field with 3
bytes");
+}
+
+TEST(FieldReaderInvalidInputTest, Int32) {
+ nanoarrow::UniqueSchema schema;
+ ASSERT_EQ(NANOARROW_OK, ArrowSchemaInitFromType(schema.get(),
NANOARROW_TYPE_INT32));
+ auto reader =
std::make_unique<PostgresCopyNetworkEndianFieldReader<int32_t>>();
+ nanoarrow::UniqueArray array;
+ ASSERT_NO_FATAL_FAILURE(InitReaderForTest(reader.get(), schema.get(),
&array));
+
+ CheckTruncatedPayload(reader.get(), array.get(), 4);
+ CheckZeroPayload(reader.get(), array.get(), 4, RawBytes<int32_t>(0));
+ CheckNullPayload(reader.get(), array.get(), 4, -1);
+ CheckNullPayload(reader.get(), array.get(), 4, 0);
+ CheckInvalidFieldSize(reader.get(), array.get(), 3,
+ "Expected field with 4 bytes but found field with 3
bytes");
+ CheckInvalidFieldSize(reader.get(), array.get(), 5,
+ "Expected field with 4 bytes but found field with 5
bytes");
+}
+
+TEST(FieldReaderInvalidInputTest, Int64) {
+ nanoarrow::UniqueSchema schema;
+ ASSERT_EQ(NANOARROW_OK, ArrowSchemaInitFromType(schema.get(),
NANOARROW_TYPE_INT64));
+ auto reader =
std::make_unique<PostgresCopyNetworkEndianFieldReader<int64_t>>();
+ nanoarrow::UniqueArray array;
+ ASSERT_NO_FATAL_FAILURE(InitReaderForTest(reader.get(), schema.get(),
&array));
+
+ CheckTruncatedPayload(reader.get(), array.get(), 8);
+ CheckZeroPayload(reader.get(), array.get(), 8, RawBytes<int64_t>(0));
+ CheckNullPayload(reader.get(), array.get(), 8, -1);
+ CheckNullPayload(reader.get(), array.get(), 8, 0);
+ CheckInvalidFieldSize(reader.get(), array.get(), 7,
+ "Expected field with 8 bytes but found field with 7
bytes");
+ CheckInvalidFieldSize(reader.get(), array.get(), 9,
+ "Expected field with 8 bytes but found field with 9
bytes");
+}
+
+TEST(FieldReaderInvalidInputTest, Float) {
+ nanoarrow::UniqueSchema schema;
+ ASSERT_EQ(NANOARROW_OK, ArrowSchemaInitFromType(schema.get(),
NANOARROW_TYPE_FLOAT));
+ auto reader =
std::make_unique<PostgresCopyNetworkEndianFieldReader<uint32_t>>();
+ nanoarrow::UniqueArray array;
+ ASSERT_NO_FATAL_FAILURE(InitReaderForTest(reader.get(), schema.get(),
&array));
+
+ CheckTruncatedPayload(reader.get(), array.get(), 4);
+ CheckZeroPayload(reader.get(), array.get(), 4, RawBytes<uint32_t>(0));
+ CheckNullPayload(reader.get(), array.get(), 4, -1);
+ CheckNullPayload(reader.get(), array.get(), 4, 0);
+ CheckInvalidFieldSize(reader.get(), array.get(), 3,
+ "Expected field with 4 bytes but found field with 3
bytes");
+ CheckInvalidFieldSize(reader.get(), array.get(), 5,
+ "Expected field with 4 bytes but found field with 5
bytes");
+}
+
+TEST(FieldReaderInvalidInputTest, Double) {
+ nanoarrow::UniqueSchema schema;
+ ASSERT_EQ(NANOARROW_OK, ArrowSchemaInitFromType(schema.get(),
NANOARROW_TYPE_DOUBLE));
+ auto reader =
std::make_unique<PostgresCopyNetworkEndianFieldReader<uint64_t>>();
+ nanoarrow::UniqueArray array;
+ ASSERT_NO_FATAL_FAILURE(InitReaderForTest(reader.get(), schema.get(),
&array));
+
+ CheckTruncatedPayload(reader.get(), array.get(), 8);
+ CheckZeroPayload(reader.get(), array.get(), 8, RawBytes<uint64_t>(0));
+ CheckNullPayload(reader.get(), array.get(), 8, -1);
+ CheckNullPayload(reader.get(), array.get(), 8, 0);
+ CheckInvalidFieldSize(reader.get(), array.get(), 7,
+ "Expected field with 8 bytes but found field with 7
bytes");
+ CheckInvalidFieldSize(reader.get(), array.get(), 9,
+ "Expected field with 8 bytes but found field with 9
bytes");
+}
+
+TEST(FieldReaderInvalidInputTest, Date) {
+ constexpr int32_t kPostgresDateEpoch = 10957;
+ nanoarrow::UniqueSchema schema;
+ ASSERT_EQ(NANOARROW_OK, ArrowSchemaInitFromType(schema.get(),
NANOARROW_TYPE_DATE32));
+ auto reader = std::make_unique<
+ PostgresCopyNetworkEndianFieldReader<int32_t, kPostgresDateEpoch>>();
+ nanoarrow::UniqueArray array;
+ ASSERT_NO_FATAL_FAILURE(InitReaderForTest(reader.get(), schema.get(),
&array));
+
+ CheckTruncatedPayload(reader.get(), array.get(), 4);
+ CheckZeroPayload(reader.get(), array.get(), 4,
RawBytes<int32_t>(kPostgresDateEpoch));
+ CheckNullPayload(reader.get(), array.get(), 4, -1);
+ CheckNullPayload(reader.get(), array.get(), 4, 0);
+ CheckInvalidFieldSize(reader.get(), array.get(), 3,
+ "Expected field with 4 bytes but found field with 3
bytes");
+ CheckInvalidFieldSize(reader.get(), array.get(), 5,
+ "Expected field with 4 bytes but found field with 5
bytes");
+}
+
+TEST(FieldReaderInvalidInputTest, Timestamp) {
+ constexpr int64_t kPostgresTimestampEpoch = 946684800000000;
+ nanoarrow::UniqueSchema schema;
+ ArrowSchemaInit(schema.get());
+ ASSERT_EQ(NANOARROW_OK,
+ ArrowSchemaSetTypeDateTime(schema.get(), NANOARROW_TYPE_TIMESTAMP,
+ NANOARROW_TIME_UNIT_MICRO, nullptr));
+ auto reader = std::make_unique<
+ PostgresCopyNetworkEndianFieldReader<int64_t,
kPostgresTimestampEpoch>>();
+ nanoarrow::UniqueArray array;
+ ASSERT_NO_FATAL_FAILURE(InitReaderForTest(reader.get(), schema.get(),
&array));
+
+ CheckTruncatedPayload(reader.get(), array.get(), 8);
+ CheckZeroPayload(reader.get(), array.get(), 8,
+ RawBytes<int64_t>(kPostgresTimestampEpoch));
+ CheckNullPayload(reader.get(), array.get(), 8, -1);
+ CheckNullPayload(reader.get(), array.get(), 8, 0);
+ CheckInvalidFieldSize(reader.get(), array.get(), 7,
+ "Expected field with 8 bytes but found field with 7
bytes");
+ CheckInvalidFieldSize(reader.get(), array.get(), 9,
+ "Expected field with 8 bytes but found field with 9
bytes");
+}
+
+TEST(FieldReaderInvalidInputTest, Interval) {
+ nanoarrow::UniqueSchema schema;
+ ASSERT_EQ(NANOARROW_OK, ArrowSchemaInitFromType(
+ schema.get(),
NANOARROW_TYPE_INTERVAL_MONTH_DAY_NANO));
+ auto reader = std::make_unique<PostgresCopyIntervalFieldReader>();
+ nanoarrow::UniqueArray array;
+ ASSERT_NO_FATAL_FAILURE(InitReaderForTest(reader.get(), schema.get(),
&array));
+
+ const std::vector<uint8_t> expected(16, 0);
+
+ CheckTruncatedPayload(reader.get(), array.get(), 16);
+ CheckZeroPayload(reader.get(), array.get(), 16, expected);
+ CheckNullPayload(reader.get(), array.get(), 16, -1);
+ CheckNullPayload(reader.get(), array.get(), 16, 0);
+ CheckInvalidFieldSize(reader.get(), array.get(), 15,
+ "Expected field with 16 bytes but found field with 15
bytes");
+ CheckInvalidFieldSize(reader.get(), array.get(), 17,
+ "Expected field with 16 bytes but found field with 17
bytes");
+}
+
} // namespace adbcpq
diff --git a/c/driver/postgresql/copy/reader.h
b/c/driver/postgresql/copy/reader.h
index 831b846608..2e43c3bab9 100644
--- a/c/driver/postgresql/copy/reader.h
+++ b/c/driver/postgresql/copy/reader.h
@@ -73,7 +73,7 @@ inline int64_t ReadUnsafe(ArrowBufferView* data) {
template <typename T>
ArrowErrorCode ReadChecked(ArrowBufferView* data, T* out, ArrowError* error) {
- if (data->size_bytes < static_cast<int64_t>(sizeof(T))) {
+ if (data->size_bytes < static_cast<int64_t>(sizeof(T))) [[unlikely]] {
ArrowErrorSet(error, "Unexpected end of input (expected %d bytes but found
%ld)",
static_cast<int>(sizeof(T)),
static_cast<long>(data->size_bytes)); // NOLINT(runtime/int)
@@ -158,12 +158,19 @@ class PostgresCopyBooleanFieldReader : public
PostgresCopyFieldReader {
return ArrowArrayAppendNull(array, 1);
}
- if (field_size_bytes != 1) {
+ if (field_size_bytes != 1) [[unlikely]] {
ArrowErrorSet(error, "Expected field with one byte but found field with
%d bytes",
static_cast<int>(field_size_bytes)); //
NOLINT(runtime/int)
return EINVAL;
}
+ if (field_size_bytes > data->size_bytes) [[unlikely]] {
+ ArrowErrorSet(error, "Expected %d bytes of field data but got %d bytes
of input",
+ static_cast<int>(field_size_bytes),
+ static_cast<int>(data->size_bytes)); //
NOLINT(runtime/int)
+ return EINVAL;
+ }
+
int64_t bytes_required = _ArrowBytesForBits(array->length + 1);
if (bytes_required > data_->size_bytes) {
NANOARROW_RETURN_NOT_OK(
@@ -191,13 +198,20 @@ class PostgresCopyNetworkEndianFieldReader : public
PostgresCopyFieldReader {
return ArrowArrayAppendNull(array, 1);
}
- if (field_size_bytes != static_cast<int32_t>(sizeof(T))) {
+ if (field_size_bytes != static_cast<int32_t>(sizeof(T))) [[unlikely]] {
ArrowErrorSet(error, "Expected field with %d bytes but found field with
%d bytes",
static_cast<int>(sizeof(T)),
static_cast<int>(field_size_bytes)); //
NOLINT(runtime/int)
return EINVAL;
}
+ if (field_size_bytes > data->size_bytes) [[unlikely]] {
+ ArrowErrorSet(error, "Expected %d bytes of field data but got %d bytes
of input",
+ static_cast<int>(field_size_bytes),
+ static_cast<int>(data->size_bytes)); //
NOLINT(runtime/int)
+ return EINVAL;
+ }
+
T value = kOffset + ReadUnsafe<T>(data);
NANOARROW_RETURN_NOT_OK(ArrowBufferAppend(data_, &value, sizeof(T)));
return AppendValid(array);
@@ -213,18 +227,26 @@ class PostgresCopyIntervalFieldReader : public
PostgresCopyFieldReader {
return ArrowArrayAppendNull(array, 1);
}
- if (field_size_bytes != 16) {
+ if (field_size_bytes != 16) [[unlikely]] {
ArrowErrorSet(error, "Expected field with %d bytes but found field with
%d bytes",
16,
static_cast<int>(field_size_bytes)); //
NOLINT(runtime/int)
return EINVAL;
}
+ if (field_size_bytes > data->size_bytes) [[unlikely]] {
+ ArrowErrorSet(error, "Expected %d bytes of field data but got %d bytes
of input",
+ static_cast<int>(field_size_bytes),
+ static_cast<int>(data->size_bytes)); //
NOLINT(runtime/int)
+ return EINVAL;
+ }
+
// postgres stores time as usec, arrow stores as ns
const int64_t time_usec = ReadUnsafe<int64_t>(data);
int64_t time;
- if (time_usec > kMaxSafeMicrosToNanos || time_usec <
kMinSafeMicrosToNanos) {
+ if (time_usec > kMaxSafeMicrosToNanos || time_usec < kMinSafeMicrosToNanos)
+ [[unlikely]] {
ArrowErrorSet(error,
"[libpq] Interval with time value %" PRId64
" usec would overflow when converting to nanoseconds",
@@ -265,7 +287,7 @@ class PostgresCopyNumericFieldReader : public
PostgresCopyFieldReader {
}
// Read the input
- if (data->size_bytes < static_cast<int64_t>(4 * sizeof(int16_t))) {
+ if (data->size_bytes < static_cast<int64_t>(4 * sizeof(int16_t)))
[[unlikely]] {
ArrowErrorSet(error,
"Expected at least %d bytes of field data for numeric copy
data but "
"only %d bytes of input remain",
@@ -279,7 +301,7 @@ class PostgresCopyNumericFieldReader : public
PostgresCopyFieldReader {
uint16_t sign = ReadUnsafe<uint16_t>(data);
uint16_t dscale = ReadUnsafe<uint16_t>(data);
- if (data->size_bytes < static_cast<int64_t>(ndigits * sizeof(int16_t))) {
+ if (data->size_bytes < static_cast<int64_t>(ndigits * sizeof(int16_t)))
[[unlikely]] {
ArrowErrorSet(error,
"Expected at least %d bytes of field data for numeric
digits copy "
"data but only %d bytes of input remain",
@@ -318,7 +340,7 @@ class PostgresCopyNumericFieldReader : public
PostgresCopyFieldReader {
if (!special_value.empty()) {
if ((data_->size_bytes + static_cast<int64_t>(special_value.size())) >
- static_cast<int64_t>((std::numeric_limits<int32_t>::max)())) {
+ static_cast<int64_t>((std::numeric_limits<int32_t>::max)()))
[[unlikely]] {
return EOVERFLOW;
}
@@ -401,7 +423,7 @@ class PostgresCopyNumericFieldReader : public
PostgresCopyFieldReader {
// Update data buffer size and add offsets
if ((data_->size_bytes + actual_chars_required) >
- static_cast<int64_t>((std::numeric_limits<int32_t>::max)())) {
+ static_cast<int64_t>((std::numeric_limits<int32_t>::max)()))
[[unlikely]] {
return EOVERFLOW;
}
data_->size_bytes += actual_chars_required;
@@ -437,7 +459,7 @@ class PostgresCopyBinaryFieldReader : public
PostgresCopyFieldReader {
return ArrowArrayAppendNull(array, 1);
}
- if (field_size_bytes > data->size_bytes) {
+ if (field_size_bytes > data->size_bytes) [[unlikely]] {
ArrowErrorSet(error, "Expected %d bytes of field data but got %d bytes
of input",
static_cast<int>(field_size_bytes),
static_cast<int>(data->size_bytes)); //
NOLINT(runtime/int)
@@ -446,7 +468,8 @@ class PostgresCopyBinaryFieldReader : public
PostgresCopyFieldReader {
int32_t* offsets = reinterpret_cast<int32_t*>(offsets_->data);
int32_t next_offset = 0;
- if (!psnip_safe_int32_add(&next_offset, offsets[array->length],
field_size_bytes)) {
+ if (!psnip_safe_int32_add(&next_offset, offsets[array->length],
field_size_bytes))
+ [[unlikely]] {
return EOVERFLOW;
}
NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(offsets_, next_offset));
@@ -470,14 +493,14 @@ class PostgresCopyFixedSizeBinaryFieldReader : public
PostgresCopyFieldReader {
return ArrowArrayAppendNull(array, 1);
}
- if (field_size_bytes != schema_view_.fixed_size) {
+ if (field_size_bytes != schema_view_.fixed_size) [[unlikely]] {
ArrowErrorSet(error, "Expected field with %d bytes but found field with
%d bytes",
static_cast<int>(schema_view_.fixed_size),
static_cast<int>(field_size_bytes)); //
NOLINT(runtime/int)
return EINVAL;
}
- if (field_size_bytes > data->size_bytes) {
+ if (field_size_bytes > data->size_bytes) [[unlikely]] {
ArrowErrorSet(error, "Expected %d bytes of field data but got %d bytes
of input",
static_cast<int>(field_size_bytes),
static_cast<int>(data->size_bytes)); //
NOLINT(runtime/int)
@@ -505,12 +528,12 @@ class PostgresCopyJsonbFieldReader : public
PostgresCopyFieldReader {
return ArrowArrayAppendNull(array, 1);
}
- if (field_size_bytes == 0) {
+ if (field_size_bytes == 0) [[unlikely]] {
ArrowErrorSet(error, "Expected at least one byte of JSONB field data");
return EINVAL;
}
- if (field_size_bytes > data->size_bytes) {
+ if (field_size_bytes > data->size_bytes) [[unlikely]] {
ArrowErrorSet(error, "Expected %d bytes of field data but got %d bytes
of input",
static_cast<int>(field_size_bytes),
static_cast<int>(data->size_bytes)); //
NOLINT(runtime/int)
@@ -519,7 +542,7 @@ class PostgresCopyJsonbFieldReader : public
PostgresCopyFieldReader {
int8_t version;
NANOARROW_RETURN_NOT_OK(ReadChecked<int8_t>(data, &version, error));
- if (version != 1) {
+ if (version != 1) [[unlikely]] {
ArrowErrorSet(error, "Expected JSONB binary version 0x01 but got %d",
static_cast<int>(version));
return EINVAL;
@@ -529,7 +552,8 @@ class PostgresCopyJsonbFieldReader : public
PostgresCopyFieldReader {
int32_t* offsets = reinterpret_cast<int32_t*>(offsets_->data);
int32_t next_offset = 0;
- if (!psnip_safe_int32_add(&next_offset, offsets[array->length],
field_size_bytes)) {
+ if (!psnip_safe_int32_add(&next_offset, offsets[array->length],
field_size_bytes))
+ [[unlikely]] {
return EOVERFLOW;
}
NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(offsets_, next_offset));
@@ -581,7 +605,7 @@ class PostgresCopyArrayFieldReader : public
PostgresCopyFieldReader {
// We could validate the OID here, but this is a poor fit for all cases
// (e.g. testing) since the OID can be specific to each database
- if (n_dim < 0) {
+ if (n_dim < 0) [[unlikely]] {
ArrowErrorSet(error, "Expected array n_dim > 0 but got %d",
static_cast<int>(n_dim)); // NOLINT(runtime/int)
return EINVAL;
@@ -601,7 +625,7 @@ class PostgresCopyArrayFieldReader : public
PostgresCopyFieldReader {
int32_t lower_bound;
NANOARROW_RETURN_NOT_OK(ReadChecked<int32_t>(data, &lower_bound, error));
- if (lower_bound != 0 && lower_bound != 1) {
+ if (lower_bound != 0 && lower_bound != 1) [[unlikely]] {
ArrowErrorSet(error,
"Array value with lower bound not in {0, 1} is not
supported");
return EINVAL;
@@ -621,7 +645,7 @@ class PostgresCopyArrayFieldReader : public
PostgresCopyFieldReader {
}
int64_t bytes_read = data->data.as_uint8 - data0;
- if (bytes_read != field_size_bytes) {
+ if (bytes_read != field_size_bytes) [[unlikely]] {
ArrowErrorSet(error, "Expected to read %d bytes from array field but
read %d bytes",
static_cast<int>(field_size_bytes),
static_cast<int>(bytes_read)); // NOLINT(runtime/int)
@@ -674,7 +698,7 @@ class PostgresCopyRecordFieldReader : public
PostgresCopyFieldReader {
int32_t n_fields;
NANOARROW_RETURN_NOT_OK(ReadChecked<int32_t>(data, &n_fields, error));
- if (n_fields != array->n_children) {
+ if (n_fields != array->n_children) [[unlikely]] {
ArrowErrorSet(error, "Expected nested record type to have %ld fields but
got %d",
static_cast<long>(array->n_children), //
NOLINT(runtime/int)
static_cast<int>(n_fields)); //
NOLINT(runtime/int)
@@ -694,20 +718,20 @@ class PostgresCopyRecordFieldReader : public
PostgresCopyFieldReader {
// appended to. This leaves array in a valid state in the specific case
// where EOVERFLOW was returned so that a higher level caller can attempt
// to try again after creating a new array.
- if (result == EOVERFLOW) {
+ if (result == EOVERFLOW) [[unlikely]] {
for (int16_t j = 0; j < i; j++) {
array->children[j]->length--;
}
}
- if (result != NANOARROW_OK) {
+ if (result != NANOARROW_OK) [[unlikely]] {
return result;
}
}
// field size == -1 means don't check (e.g., for a top-level row tuple)
int64_t bytes_read = data->data.as_uint8 - data0;
- if (field_size_bytes != -1 && bytes_read != field_size_bytes) {
+ if (field_size_bytes != -1 && bytes_read != field_size_bytes) [[unlikely]]
{
ArrowErrorSet(error,
"Expected to read %d bytes from record field but read %d
bytes",
static_cast<int>(field_size_bytes),
@@ -757,7 +781,7 @@ class PostgresCopyFieldTupleReader : public
PostgresCopyFieldReader {
NANOARROW_RETURN_NOT_OK(ReadChecked<int16_t>(data, &n_fields, error));
if (n_fields == -1) {
return ENODATA;
- } else if (n_fields != array->n_children) {
+ } else if (n_fields != array->n_children) [[unlikely]] {
ArrowErrorSet(error,
"Expected -1 for end-of-stream or number of fields in
output array "
"(%ld) but got %d",
@@ -776,13 +800,13 @@ class PostgresCopyFieldTupleReader : public
PostgresCopyFieldReader {
// appended to. This leaves array in a valid state in the specific case
// where EOVERFLOW was returned so that a higher level caller can attempt
// to try again after creating a new array.
- if (result == EOVERFLOW) {
+ if (result == EOVERFLOW) [[unlikely]] {
for (int16_t j = 0; j < i; j++) {
array->children[j]->length--;
}
}
- if (result != NANOARROW_OK) {
+ if (result != NANOARROW_OK) [[unlikely]] {
return result;
}
}
@@ -905,7 +929,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(
case NANOARROW_TYPE_LIST:
switch (pg_type.type_id()) {
case PostgresTypeId::kArray: {
- if (pg_type.n_children() != 1) {
+ if (pg_type.n_children() != 1) [[unlikely]] {
ArrowErrorSet(
error, "Expected Postgres array type to have one child but
found %ld",
static_cast<long>(pg_type.n_children())); //
NOLINT(runtime/int)
@@ -943,7 +967,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(
case NANOARROW_TYPE_STRUCT:
switch (pg_type.type_id()) {
case PostgresTypeId::kRecord: {
- if (pg_type.n_children() != schema->n_children) {
+ if (pg_type.n_children() != schema->n_children) [[unlikely]] {
ArrowErrorSet(error,
"Can't convert Postgres record type with %ld
children to Arrow "
"struct type with %ld children",
@@ -1018,7 +1042,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(
class PostgresCopyStreamReader {
public:
ArrowErrorCode Init(PostgresType pg_type) {
- if (pg_type.type_id() != PostgresTypeId::kRecord) {
+ if (pg_type.type_id() != PostgresTypeId::kRecord) [[unlikely]] {
return EINVAL;
}
@@ -1031,7 +1055,7 @@ class PostgresCopyStreamReader {
int64_t array_size_approx_bytes() const { return array_size_approx_bytes_; }
ArrowErrorCode SetOutputSchema(ArrowSchema* schema, ArrowError* error) {
- if (std::string(schema_->format) != "+s") {
+ if (std::string(schema_->format) != "+s") [[unlikely]] {
ArrowErrorSet(
error,
"Expected output schema of type struct but got output schema with
format '%s'",
@@ -1039,7 +1063,7 @@ class PostgresCopyStreamReader {
return EINVAL;
}
- if (schema_->n_children != root_reader_.InputType().n_children()) {
+ if (schema_->n_children != root_reader_.InputType().n_children())
[[unlikely]] {
ArrowErrorSet(error,
"Expected output schema with %ld columns to match Postgres
input but "
"got schema with %ld columns",
@@ -1062,7 +1086,7 @@ class PostgresCopyStreamReader {
}
ArrowErrorCode InitFieldReaders(ArrowError* error) {
- if (schema_->release == nullptr) {
+ if (schema_->release == nullptr) [[unlikely]] {
return EINVAL;
}
@@ -1081,7 +1105,8 @@ class PostgresCopyStreamReader {
}
ArrowErrorCode ReadHeader(ArrowBufferView* data, ArrowError* error) {
- if (data->size_bytes <
static_cast<int64_t>(sizeof(kPgCopyBinarySignature))) {
+ if (data->size_bytes <
static_cast<int64_t>(sizeof(kPgCopyBinarySignature)))
+ [[unlikely]] {
ArrowErrorSet(
error,
"Expected PGCOPY signature of %ld bytes at beginning of stream but "
@@ -1092,7 +1117,7 @@ class PostgresCopyStreamReader {
}
if (memcmp(data->data.data, kPgCopyBinarySignature,
sizeof(kPgCopyBinarySignature)) !=
- 0) {
+ 0) [[unlikely]] {
ArrowErrorSet(error, "Invalid PGCOPY signature at beginning of stream");
return EINVAL;
}
@@ -1105,7 +1130,7 @@ class PostgresCopyStreamReader {
uint32_t extension_length;
NANOARROW_RETURN_NOT_OK(ReadChecked<uint32_t>(data, &extension_length,
error));
- if (data->size_bytes < static_cast<int64_t>(extension_length)) {
+ if (data->size_bytes < static_cast<int64_t>(extension_length))
[[unlikely]] {
ArrowErrorSet(error,
"Expected %ld bytes of extension metadata at start of
stream but "
"found %ld bytes of input",
@@ -1139,7 +1164,7 @@ class PostgresCopyStreamReader {
}
ArrowErrorCode GetArray(ArrowArray* out, ArrowError* error) {
- if (array_->release == nullptr) {
+ if (array_->release == nullptr) [[unlikely]] {
return EINVAL;
}