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)