Skip to content

fix: Never pass column names or server messages as a printf format - #665

Merged
krlmlr merged 2 commits into
mainfrom
claude/fix-format-strings
Nov 29, 2026
Merged

krlmlr merged 2 commits into
mainfrom
claude/fix-format-strings

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #664.

libpq error messages and the type-conflict warning in DbColumn::warn_type_conflicts() reached cpp11::stop() and cpp11::warning() as the format string, so a % in a database, table, column or file name was interpreted by R. This PR passes the text as a "%s" argument at all six call sites listed in #664: src/DbColumn.cpp line 79 and src/DbConnection.cpp lines 36, 109, 135, 143 and 291. Each edit changes only the single call line, so that merges with other open PRs stay trivial. The "Query requires %i params; %i supplied." error in src/PqResultImpl.cpp also received params.size(), an R_xlen_t, for an %i; it now uses %ld with a cast to long. Those lines lie outside the hunks of #638.

Call sites in files that other open PRs touch: src/DbColumn.cpp (the cpp11::warning() line in warn_type_conflicts()) is also edited by #659, which adds a warn_parse_failures() call in the same function; its own new warning already uses "%s". src/PqResultImpl.cpp is also edited by #638, in hunks that do not overlap the parameter count error. None of the format string fixes are in src/PqDataFrame.* (#638) or src/PqColumnDataSource.cpp (#659, #660).

The new tests/testthat/test-format-string.R checks that hostile names such as db%s%d%x appear verbatim in the errors from dbConnect(), from a failing query (relation and column names), and from postgresImportLargeObject() and postgresExportLargeObject(). The tests skip when no server is reachable and remove the large object they import. Without the fix three of the four tests fail with garbled messages. The full devtools::test() passes against a local PostgreSQL 16 in a fresh database.

Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g

🤖 Generated with Claude Code

https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g

Error messages from libpq and the type-conflict warning reached
`cpp11::stop()` and `cpp11::warning()` as the format string.
A `%` in a database, table, column or file name was then interpreted by R,
which garbled the message or read arbitrary memory.
The text is now passed as a `"%s"` argument.

Closes #664.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
`params.size()` is an `R_xlen_t`,
so the error now formats it with `%ld` after a cast to `long`.
The new tests also skip the connection check when no server is reachable,
and remove the large object they import.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 12:45
@krlmlr krlmlr mentioned this pull request Nov 29, 2026
3 of 9 tasks

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

codecov/patch reports 71.42% of the diff covered against a target of 83.13%. The diff changes only single format-argument lines, and a few of them sit on paths that tests can't reach:

  • the type-conflict warning in DbColumn::warn_type_conflicts(): conflicting column types stop earlier with "Multiple queries must use the same column types", as the issue explains;
  • the PQcancel() failure in DbConnection.cpp, which needs a cancel request to fail.

The other changed lines are covered by tests/testthat/test-format-string.R: the connect error, conn_stop() (all server errors) and the large-object import and export. No coverage is missing on a reachable path.


Generated by Claude Code

@krlmlr
krlmlr merged commit c69cb46 into main Nov 29, 2026
8 of 9 checks passed
@krlmlr
krlmlr deleted the claude/fix-format-strings branch November 29, 2026 15:55
krlmlr added a commit that referenced this pull request Nov 29, 2026
…666)

* feat: Expose the PostgreSQL SQLSTATE and diagnostic fields on errors

Server errors now raise a classed rlang error,
`c("RPostgres_error_<SQLSTATE>", "RPostgres_error", "rlang_error", "error", "condition")`,
that carries the SQLSTATE and the other libpq diagnostic fields.
A `rethrow()` wrapper around the C++ entry points restores the calling context.

Ported from #581 onto the current main.
The one conflict, in `DbConnection::conn_stop()`, is resolved in favor of the new `result_stop()`,
whose fallback now passes the message as a `"%s"` argument as #665 requires.

Co-authored-by: Kirill Müller <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* fix: Carry the SQLSTATE when a COPY fails to start

`dbAppendTable()` and `dbWriteTable()` with `copy = TRUE` now pass the result of the failing `COPY ... FROM STDIN` to `result_stop()`,
so an unknown column or a missing table also carries its SQLSTATE.
`result_stop()` is declared `[[noreturn]]`,
which documents that the `PQclear()` after each call is never reached for the result it owns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* fix: Name the DBI verb in errors from `dbAppendTable()` and `dbWriteTable()`

`rethrow()` finds the call from the frame of its caller and skips the `.local()` wrapper that S4 adds,
which also works when the C++ entry point sits in a helper such as `db_append_table()`.
Its comment now explains why the condition survives the trip through C++.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* docs: Describe which errors carry the SQLSTATE

Require rlang 1.0.0 for `rlang::try_fetch()`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* test: Cover the COPY, bind, fetch and lost-connection paths

The snapshot sets `lc_messages` to C so that the server's text does not depend on its locale,
and skips where the role may not set it.
The redundant `local_edition(3)` is gone, since DESCRIPTION selects edition 3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* test: Wait for the terminated backend before using its connection

`pg_terminate_backend()` returns once it has signalled the backend,
so the next query could still reach it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* fix: Release the failing result before allocating the R condition

`result_stop()` copies the diagnostic fields into C++ strings and calls `PQclear()` first,
so an R error while building the field list cannot leak the `PGresult`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* fix: Keep libpq's own messages in the native encoding

A message from the server is UTF-8, the client encoding RPostgres sets,
but one that libpq produces itself, such as for a lost connection, is native,
as `cpp11::stop()` treated it before.
`signal_pq_error()` is now looked up on every error instead of cached in a static,
so a reloaded namespace is honored.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* perf: Rethrow with base `withCallingHandlers()`

`rlang::try_fetch()` added about 12 microseconds to every `dbFetch()`; the base function adds about 2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* docs: Name the internal DBI calls and the errors without the class

`?RPostgres-conditions` says that `dbGetQuery()` and `dbExecute()` report `dbSendQuery()` or `dbBind()`,
and that large object import and export errors stay plain.
A test checks the diagnostic fields with `lc_messages` set to C.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

* feat: Print the server's primary message with the diagnostic fields as bullets

A server error now reads as the failing step and the primary message,
followed by an `i` bullet for each field that is set:
SQLSTATE, detail, hint, position, context, schema, table, column, data type and constraint.
These replace the DETAIL, HINT and CONTEXT lines of libpq's text, so nothing is printed twice.
The condition gains a `position` field.
Errors that libpq produced itself, recognized by the missing SQLSTATE, keep their text and stay in the native encoding.

Snapshot tests with `lc_messages` set to C show the output for undefined objects, syntax and type errors,
unique, not-null, check and domain violations, COPY, PL/pgSQL with a custom code, and a context over several lines.
Another test pins what `conditionMessage()` returns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

---------

Co-authored-by: David Schoch <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

Server messages, database names and file names are passed to R as a printf format

2 participants