Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(c/driver/postgresql): check for underflow #1389

Merged
merged 2 commits into from
Dec 20, 2023
Merged
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
19 changes: 16 additions & 3 deletions c/driver/postgresql/postgres_copy_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

#pragma once

// Windows
#define NOMINMAX

#include <algorithm>
#include <cerrno>
#include <cinttypes>
#include <cstdint>
#include <limits>
#include <memory>
#include <string>
#include <utility>
Expand Down Expand Up @@ -68,6 +72,9 @@ constexpr int64_t kMaxSafeMicrosToNanos = 9223372036854775L;
// without overflow
constexpr int64_t kMinSafeMicrosToNanos = -9223372036854775L;

// 2000-01-01 00:00:00.000000 in microseconds
constexpr int64_t kPostgresTimestampEpoch = 946684800000000L;

// Read a value from the buffer without checking the buffer size. Advances
// the cursor of data and reduces its size by sizeof(T).
template <typename T>
Expand Down Expand Up @@ -1333,14 +1340,20 @@ class PostgresCopyTimestampFieldWriter : public PostgresCopyFieldWriter {

if (!overflow_safe) {
ArrowErrorSet(error,
"Row %" PRId64 " timestamp value %" PRId64
"[libpq] Row %" PRId64 " timestamp value %" PRId64
" with unit %d would overflow",
index, raw_value, TU);
return ADBC_STATUS_INVALID_ARGUMENT;
}

// 2000-01-01 00:00:00.000000 in microseconds
constexpr int64_t kPostgresTimestampEpoch = 946684800000000;
if (value < std::numeric_limits<int64_t>::min() + kPostgresTimestampEpoch) {
ArrowErrorSet(error,
"[libpq] Row %" PRId64 " timestamp value %" PRId64
" with unit %d would underflow",
index, raw_value, TU);
return ADBC_STATUS_INVALID_ARGUMENT;
}

const int64_t scaled = value - kPostgresTimestampEpoch;
NANOARROW_RETURN_NOT_OK(WriteChecked<int64_t>(buffer, scaled, error));

Expand Down
15 changes: 13 additions & 2 deletions c/driver/postgresql/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
// specific language governing permissions and limitations
// under the License.

// Windows
#define NOMINMAX

#include "statement.h"

#include <array>
Expand All @@ -23,6 +26,7 @@
#include <cinttypes>
#include <cstring>
#include <iostream>
#include <limits>
#include <memory>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -432,8 +436,6 @@ struct BindStream {
case ArrowType::NANOARROW_TYPE_TIMESTAMP: {
int64_t val = array_view->children[col]->buffer_views[1].data.as_int64[row];

// 2000-01-01 00:00:00.000000 in microseconds
constexpr int64_t kPostgresTimestampEpoch = 946684800000000;
bool overflow_safe = true;

auto unit = bind_schema_fields[col].time_unit;
Expand Down Expand Up @@ -469,6 +471,15 @@ struct BindStream {
return ADBC_STATUS_INVALID_ARGUMENT;
}

if (val < std::numeric_limits<int64_t>::min() + kPostgresTimestampEpoch) {
SetError(error,
"[libpq] Field #%" PRId64 " ('%s') Row #%" PRId64
" has value '%" PRIi64 "' which would underflow",
col + 1, bind_schema->children[col]->name, row + 1,
array_view->children[col]->buffer_views[1].data.as_int64[row]);
return ADBC_STATUS_INVALID_ARGUMENT;
}

if (bind_schema_fields[col].type == ArrowType::NANOARROW_TYPE_TIMESTAMP) {
const uint64_t value = ToNetworkInt64(val - kPostgresTimestampEpoch);
std::memcpy(param_values[col], &value, sizeof(int64_t));
Expand Down
Loading