pg.ddx.io  GitHub: postgresql-interfaces/psqlodbc (issues + PRs as email threads)  
help / color / mirror / Atom feed
From: pacman82 (@pacman82) <noreply+pacman82@github.com>
To: postgresql-interfaces/psqlodbc <noreply+postgresql-interfaces-psqlodbc@github.com>
Subject: [postgresql-interfaces/psqlodbc] issue #229: SQLDriverConnect does not report truncation of the output connection string
Date: Fri, 30 Oct 2026 22:42:10 +0000
Message-ID: <gh-postgresql-interfaces-psqlodbc-229@github.com> (raw)

Hello,

I am the maintainer of the `odbc-api` Rust ODBC bindings. After updating a GitHub runner to the latest Ubuntu 26 I caught a regression in the psqlODBC 17 driver. Same test worked fine using psqlODBC 16.

I let Claude investigate the root cause of the issue. Here is what it found:

### Description

When `SQLDriverConnect` is called with an output buffer (`cbConnStrOutMax`) that is too small for the completed connection string, the driver used to return `SQL_SUCCESS_WITH_INFO` with SQLSTATE `01004` and set `*pcbConnStrOut` to the length of the string. Since 17.00.0005 it returns `SQL_SUCCESS`, sets `*pcbConnStrOut` to `0` and writes an empty string. The caller cannot tell that truncation happened.

The ODBC spec for `SQLDriverConnect` requires that `StringLength2Ptr` holds the total number of characters available (excluding the null terminator), and that truncation is reported with `SQL_SUCCESS_WITH_INFO` / `01004`.

### Reproduction

```c
SQLCHAR out[1024];
SQLSMALLINT outlen = -999;
SQLRETURN rc = SQLDriverConnect(dbc, NULL,
    (SQLCHAR *)"Driver={PostgreSQL UNICODE};Server=pg;Port=5432;Database=test;Uid=test;Pwd=test;",
    SQL_NTS, out, /* cbConnStrOutMax */ 2, &outlen, SQL_DRIVER_NOPROMPT);
```

| Environment | rc | outlen | diagnostic |
|---|---|---|---|
| Ubuntu 24.04, psqlODBC 16.00.0000, unixODBC 2.3.12 | 1 (`SQL_SUCCESS_WITH_INFO`) | 143 | `01004` The buffer was too small for the ConnStrOut. |
| Ubuntu 26.04, psqlODBC 17.00.0007, unixODBC 2.3.14 | 0 (`SQL_SUCCESS`) | 0 | none |

The result is the same for `cbConnStrOutMax = 10`, and for both the ANSI and the Unicode driver. With `cbConnStrOutMax = 1024` both versions behave correctly.

### Cause

Commit 790a1f0 (#106) changed `makeConnectString` to write at most `len` bytes. If the string does not fit, it now sets `connect_string[0] = '\0'` and returns.

`PGAPI_DriverConnect` (drvconn.c) passes `cbConnStrOutMax` as `len`, even though its local buffer `connStrOut` is `MAX_CONNECT_STRING` bytes. It then uses `strlen(connStrOut)` as the full length:

```c
lenStrout = cbConnStrOutMax;
...
makeConnectString(connStrOut, ci, lenStrout);
len = strlen(connStrOut);          // 0 if it did not fit
...
if (len >= cbConnStrOutMax)        // never true any more
{ ... SQL_SUCCESS_WITH_INFO / CONN_TRUNCATED ... }
...
*pcbConnStrOut = (SQLSMALLINT) len;
```

As a result, the truncation branch can no longer be reached.

### Possible fix

Build the string into the full internal buffer (`sizeof(connStrOut)`) and let the existing truncation logic in `PGAPI_DriverConnect` handle the copy to the caller's buffer. Note that `makeConnectString` also uses `len` to decide whether to abbreviate (`len < 1024`). That decision probably needs to keep being based on `cbConnStrOutMax`, for example through a separate parameter.

Message-ID: <gh-postgresql-interfaces-psqlodbc-229@github.com>
Permalink:  ../gh-postgresql-interfaces-psqlodbc-229@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