pg.ddx.io  GitHub: postgresql-interfaces/psqlodbc (issues + PRs as email threads)  
help / color / mirror / Atom feed
[postgresql-interfaces/psqlodbc] issue #223: `CC_from_PGresult()` in `connection.c` can dereference a NULL `ConnectionClass *conn` and segfault.
2+ messages / 2 participants
[nested] [flat]

* [postgresql-interfaces/psqlodbc] issue #223: `CC_from_PGresult()` in `connection.c` can dereference a NULL `ConnectionClass *conn` and segfault.
@ 2026-10-23 18:20  "SupperSu (@SupperSu)" <noreply+SupperSu@github.com>
  0 siblings, 0 replies; 2+ messages in thread

From: SupperSu (@SupperSu) @ 2026-10-23 18:20 UTC (permalink / raw)
  To: postgresql-interfaces/psqlodbc <noreply+postgresql-interfaces-psqlodbc@github.com>

## Summary
`CC_from_PGresult()` in `connection.c` can dereference a NULL `ConnectionClass *conn` and segfault. On the cursor "fetch next batch" path, `QR_next_tuple()` calls into `CC_send_query_append()`, which invokes `CC_from_PGresult(res, stmt, NULL, cursor, &pgres)` with a hard-coded `NULL` connection. If `QR_from_PGresult()` then returns `FALSE`, the error branch runs `CC_get_errornumber(conn)` with `conn == NULL`, crashing the process with a read through a near-NULL address. The crash is in the error-handling path, so a recoverable fetch failure is turned into a hard process crash.

## Environment
- psqlODBC: observed with **12.01.0000**; the same code path is **still present on `main`** (checked HEAD, Oct 2026).
- Unicode driver (`psqlodbcw.so`), Linux x86_64, with unixODBC.
- `UseDeclareFetch` enabled (cursor-based fetch), reading from a PostgreSQL source via `SQLFetchScroll`/`SQLExtendedFetch`.

## Affected code (links to `main`)
- Crash — unguarded deref in the failure branch: https://github.com/postgresql-interfaces/psqlodbc/blob/main/connection.c#L1702-L1716 (the `if (0 >= CC_get_errornumber(conn))` at L1711, and `switch (QR_get_rstatus(res))` at L1713).
- The call site that passes `NULL` for `conn` (comment: *"called from QR_next_tuple and must return immediately"*): https://github.com/postgresql-interfaces/psqlodbc/blob/main/connection.c#L2258-L2261
- The cursor fetch-continuation that reaches it (`qi.result_in = self`): https://github.com/postgresql-interfaces/psqlodbc/blob/main/qresult.c#L1174-L1176
- `QR_from_PGresult()` deliberately tolerates a NULL `conn` (guards it before use, and its failure returns never touch `conn`): https://github.com/postgresql-interfaces/psqlodbc/blob/main/qresult.c#L598 (also the `if (stmt && conn)` and `if (NULL != conn)` guards further down). The caller `CC_from_PGresult` is missing the same guard.

## Root cause
`QR_from_PGresult()` treats `conn` as optional (it is called with `NULL` on the `QR_next_tuple` continuation and guards every use). But `CC_from_PGresult()`'s failure branch does not apply the same guard: it calls `CC_get_errornumber(conn)` (and `QR_get_rstatus(res)` / `QR_get_message(res)`) unconditionally. When `QR_from_PGresult()` fails on the NULL-`conn` path, `CC_get_errornumber(NULL)` dereferences NULL.

The faulting access is a read at the small fixed offset of the `__error_number` field within `ConnectionClass` (i.e. `NULL + offsetof(...)`), consistent with `CC_get_errornumber(conn)` on `conn == NULL`.

## Captured backtrace (production core dump, symbolized — driver frames)
Symbolized backtrace from a real crash with this signature. Frames above the ODBC entry point are application-side and omitted:

```
#0  CC_from_PGresult       psqlodbcw.so        <- SIGSEGV: CC_get_errornumber(conn) with conn == NULL
#1  CC_send_query_append   psqlodbcw.so        <- CC_from_PGresult(res, stmt, NULL, cursor, &pgres)
#2  QR_next_tuple          psqlodbcw.so        <- "FETCH n IN cursor", reuse result (qi.result_in = self)
#3  SC_fetch               psqlodbcw.so
#4  PGAPI_ExtendedFetch    psqlodbcw.so
#5  SQLFetchScroll         psqlodbcw.so
#6  SQLFetchScroll         SQLFetchScroll.c    (unixODBC driver manager)
    ... application fetch loop calling SQLFetchScroll (omitted) ...
```

The crash signature is a `SIGSEGV` with a read fault at the low fixed address noted above, and it is deterministic — every occurrence lands on the same instruction in `CC_from_PGresult`. This backtrace matches the source analysis exactly: crash at `#0 CC_from_PGresult`, reached from the `QR_next_tuple` cursor fetch-continuation (`#2 → #1`).

## How to reproduce
With `UseDeclareFetch` enabled, run a `SELECT` that opens a cursor and fetch rows so the driver issues repeated `FETCH … IN "cursor"` via `QR_next_tuple()`. If `QR_from_PGresult()` returns `FALSE` on one of the continuation fetches (e.g. the backend returns an error/FATAL for that `FETCH`, the connection is interrupted, or result construction otherwise fails), the driver enters the `CC_from_PGresult` failure branch with `conn == NULL` and crashes.

I observed this deterministically in production under concurrent bulk reads of a table with large text columns — once the failed-fetch condition occurs, it crashes at the same instruction every time. I have not reduced it to a standalone minimal reproducer, but the `NULL` originates at a fixed call site in the driver itself (connection.c L2261), so the path is unambiguous from the source.

I'm happy to open a PR with the fix if that's welcome.

^ permalink  raw  [nested|flat] 2+ messages in thread

* Re: [postgresql-interfaces/psqlodbc] issue #223: `CC_from_PGresult()` in `connection.c` can dereference a NULL `ConnectionClass *conn` and segfault.
@ 2026-10-26 04:34  "davecramer (@davecramer)" <noreply+davecramer@github.com>
  0 siblings, 0 replies; 2+ messages in thread

From: davecramer (@davecramer) @ 2026-10-26 04:34 UTC (permalink / raw)
  To: postgresql-interfaces/psqlodbc <noreply+postgresql-interfaces-psqlodbc@github.com>

@SupperSu please do

^ permalink  raw  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-10-26 04:34 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-23 18:20 [postgresql-interfaces/psqlodbc] issue #223: `CC_from_PGresult()` in `connection.c` can dereference a NULL `ConnectionClass *conn` and segfault. "SupperSu (@SupperSu)" <noreply+SupperSu@github.com>
2026-10-26 04:34 Re: [postgresql-interfaces/psqlodbc] issue #223: `CC_from_PGresult()` in `connection.c` can dereference a NULL `ConnectionClass *conn` and segfault. "davecramer (@davecramer)" <noreply+davecramer@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox