Repository navigation
fix: Never pass column names or server messages as a printf format - #665
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 29, 2026 12:45
3 of 9 tasks
Member
Author
|
The other changed lines are covered by Generated by Claude Code |
This was referenced Nov 29, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #664.
libpq error messages and the type-conflict warning in
DbColumn::warn_type_conflicts()reachedcpp11::stop()andcpp11::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.cppline 79 andsrc/DbConnection.cpplines 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 insrc/PqResultImpl.cppalso receivedparams.size(), anR_xlen_t, for an%i; it now uses%ldwith a cast tolong. Those lines lie outside the hunks of #638.Call sites in files that other open PRs touch:
src/DbColumn.cpp(thecpp11::warning()line inwarn_type_conflicts()) is also edited by #659, which adds awarn_parse_failures()call in the same function; its own new warning already uses"%s".src/PqResultImpl.cppis also edited by #638, in hunks that do not overlap the parameter count error. None of the format string fixes are insrc/PqDataFrame.*(#638) orsrc/PqColumnDataSource.cpp(#659, #660).The new
tests/testthat/test-format-string.Rchecks that hostile names such asdb%s%d%xappear verbatim in the errors fromdbConnect(), from a failing query (relation and column names), and frompostgresImportLargeObject()andpostgresExportLargeObject(). 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 fulldevtools::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