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): Fix segfault associated with uninitialized copy_reader_ #964

Merged
merged 12 commits into from
Aug 10, 2023
Merged
17 changes: 13 additions & 4 deletions c/driver/postgresql/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ struct BindStream {
} // namespace

int TupleReader::GetSchema(struct ArrowSchema* out) {
if (copy_reader_ == nullptr) {
StringBuilderAppend(&error_builder_,
"[libpq] Copy reader not initialized before calling GetSchema");
ywc88 marked this conversation as resolved.
Show resolved Hide resolved
return EINVAL;
}

int na_res = copy_reader_->GetSchema(out);
if (out->release == nullptr) {
StringBuilderAppend(&error_builder_,
Expand Down Expand Up @@ -628,7 +634,7 @@ void TupleReader::ResetQuery() {
}

int TupleReader::GetNext(struct ArrowArray* out) {
if (!copy_reader_) {
if (is_finished_) {
out->release = nullptr;
return 0;
}
Expand Down Expand Up @@ -662,9 +668,6 @@ int TupleReader::GetNext(struct ArrowArray* out) {
struct ArrowArray tmp;
NANOARROW_RETURN_NOT_OK(BuildOutput(&tmp, &error));

// Clear the copy reader to mark this reader as finished
copy_reader_.reset();

ywc88 marked this conversation as resolved.
Show resolved Hide resolved
// Check the server-side response
result_ = PQgetResult(conn_);
const int pq_status = PQresultStatus(result_);
Expand Down Expand Up @@ -696,6 +699,12 @@ void TupleReader::Release() {
PQfreemem(pgbuf_);
pgbuf_ = nullptr;
}

// Clear the copy reader to mark this reader as finished
ywc88 marked this conversation as resolved.
Show resolved Hide resolved
if (copy_reader_) {
copy_reader_.reset();
is_finished_ = true;
}
}

void TupleReader::ExportTo(struct ArrowArrayStream* stream) {
Expand Down
2 changes: 2 additions & 0 deletions c/driver/postgresql/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class TupleReader final {
StringBuilderInit(&error_builder_, 0);
data_.data.as_char = nullptr;
data_.size_bytes = 0;
is_finished_ = false;
ywc88 marked this conversation as resolved.
Show resolved Hide resolved
}

int GetSchema(struct ArrowSchema* out);
Expand Down Expand Up @@ -85,6 +86,7 @@ class TupleReader final {
std::unique_ptr<PostgresCopyStreamReader> copy_reader_;
int64_t row_id_;
int64_t batch_size_hint_bytes_;
bool is_finished_;
};

class PostgresStatement {
Expand Down
Loading