From af1f6525fddf8774a9cccd792e049ace5f0c6238 Mon Sep 17 00:00:00 2001 From: Rex Date: Fri, 20 Nov 2026 09:18:45 +0800 Subject: [PATCH] Fix GH-25390: pdo_odbc allocates the declared column size for long text columns When the driver returns SQL_NO_TOTAL for a long column, the fix for GH-23443 (a4c4173d9a) started using the declared column size (SQL_DESC_DISPLAY_SIZE) as the block size for the remaining SQLGetData() calls. For SQL Server text / ntext the declared size is 2 GB / 1 GB, so fetching any value that does not fit the first buffer tried to allocate that much and the process was killed by the memory limit. The declared size is an upper bound of the type, not of the value, so it cannot be used as an allocation size when the total is unknown. Read block by block (LONG_COLUMN_BUFFER_SIZE - 1) whenever the driver cannot tell the total length, as the code did before a4c4173d9a for large declared sizes; that path already handles the declared size 0 case of GH-23443. Fix suggested by licvido in the issue. Co-Authored-By: Claude Fable 5.1 --- NEWS | 5 +++++ ext/pdo_odbc/odbc_stmt.c | 10 ++++++---- ext/pdo_odbc/tests/gh25390.phpt | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 ext/pdo_odbc/tests/gh25390.phpt diff --git a/NEWS b/NEWS index 844ee50419fd..80e5e1899760 100644 --- a/NEWS +++ b/NEWS @@ -119,6 +119,11 @@ PHP NEWS . Fixed a crash when executing a prepared statement again after a failed execute() with libmysqlclient. (Ilia Alshanetsky) +- PDO_ODBC: + . Fixed bug GH-25390 (fetching a long text / ntext value allocates the + declared column size when the driver returns SQL_NO_TOTAL). (licvido, + chep6915) + - POSIX: . Fixed bug GH-25530 (posix_get_last_error() returns an error from a previous request). (Artem Ukrainskiy) diff --git a/ext/pdo_odbc/odbc_stmt.c b/ext/pdo_odbc/odbc_stmt.c index d30979651d3d..d401e216d262 100644 --- a/ext/pdo_odbc/odbc_stmt.c +++ b/ext/pdo_odbc/odbc_stmt.c @@ -775,13 +775,15 @@ static int odbc_stmt_get_col(pdo_stmt_t *stmt, int colno, zval *result, enum pdo * changed from 256 byte to LONG_COLUMN_BUFFER_SIZE. */ ssize_t to_fetch_len; - if (orig_fetched_len == SQL_NO_TOTAL && C->datalen > (LONG_COLUMN_BUFFER_SIZE - 1)) { - to_fetch_len = C->datalen; - } else if (orig_fetched_len > 0) { + if (orig_fetched_len > 0) { /* implicitly not SQL_NO_TOTAL, should be OK */ to_fetch_len = orig_fetched_len; } else { - /* size must be > 0 to actually get data */ + /* SQL_NO_TOTAL: the total length is unknown, so read block by + * block. C->datalen is the declared size of the column (e.g. 2 GB + * for text), not the size of the value, so it must not be used as + * the allocation size here (GH-25390). The block size must be > 0 + * to actually get data (GH-23443). */ to_fetch_len = (LONG_COLUMN_BUFFER_SIZE - 1); } ssize_t to_fetch_byte = to_fetch_len + 1; diff --git a/ext/pdo_odbc/tests/gh25390.phpt b/ext/pdo_odbc/tests/gh25390.phpt new file mode 100644 index 000000000000..f2bfd4c2d7c5 --- /dev/null +++ b/ext/pdo_odbc/tests/gh25390.phpt @@ -0,0 +1,33 @@ +--TEST-- +GH-25390 (fetching a long text / ntext value allocates the declared column size when the driver returns SQL_NO_TOTAL) +--EXTENSIONS-- +pdo_odbc +--SKIPIF-- + +--INI-- +memory_limit=256M +--FILE-- +query("SELECT CAST(REPLICATE(CAST('A' AS varchar(max)), $n) AS text) AS v")->fetch(PDO::FETCH_ASSOC); + var_dump(strlen($row['v'])); + $row = $db->query("SELECT CAST(REPLICATE(CAST(N'A' AS nvarchar(max)), $n) AS ntext) AS v")->fetch(PDO::FETCH_ASSOC); + var_dump(strlen($row['v'])); +} +?> +--EXPECT-- +int(4073) +int(4073) +int(300000) +int(300000)