Skip to content

Fix two libpq leak bugs on COPY error paths (1.5) - #556

Merged
staticlibs merged 1 commit into
duckdb:v1.5-variegatafrom
staticlibs:libpq_leaks_15
Aug 21, 2026
Merged

Fix two libpq leak bugs on COPY error paths (1.5)#556
staticlibs merged 1 commit into
duckdb:v1.5-variegatafrom
staticlibs:libpq_leaks_15

Conversation

@staticlibs

Copy link
Copy Markdown
Member

This is a backport of the PR #555 to v1.5-variegata stable branch.

PostgresBinaryReader::FetchNextBuffer() leaked the PQgetCopyData buffer when the message was shorter than the tuple count it has to contain. PQgetCopyData allocates even when it hands back a length we cannot use, and buffer was only assigned after the check, so FreeBuffer() never saw it. Assign it first and let the existing ownership do the freeing.

The three COPY error paths passed a possibly null PGresult to PQresultErrorMessage(). libpq returns an empty string rather than crashing, which is how "Failed to copy data: " with no reason after it gets produced. A null result means the connection failed, so report PQerrorMessage() in that case - the same pattern ExecuteQueries() and PostgresQueryBind() already use.

Reported as Snowflake-Labs/pg_lake#415.

This is a backport of the PR duckdb#555 to `v1.5-variegata` stable branch.

PostgresBinaryReader::FetchNextBuffer() leaked the PQgetCopyData buffer
when the message was shorter than the tuple count it has to contain.
PQgetCopyData allocates even when it hands back a length we cannot use,
and buffer was only assigned after the check, so FreeBuffer() never saw
it. Assign it first and let the existing ownership do the freeing.

The three COPY error paths passed a possibly null PGresult to
PQresultErrorMessage(). libpq returns an empty string rather than
crashing, which is how "Failed to copy data: " with no reason after it
gets produced. A null result means the connection failed, so report
PQerrorMessage() in that case - the same pattern ExecuteQueries() and
PostgresQueryBind() already use.

Reported as Snowflake-Labs/pg_lake#415.
@staticlibs
staticlibs merged commit 0027dfc into duckdb:v1.5-variegata Aug 21, 2026
7 checks passed
@staticlibs
staticlibs deleted the libpq_leaks_15 branch August 21, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants