Skip to content

Fix GH-25390: pdo_odbc allocates the declared column size for long text columns - #25616

Open
chep6915 wants to merge 1 commit into
php:PHP-8.5from
chep6915:fix-gh25390
Open

chep6915 wants to merge 1 commit into
php:PHP-8.5from
chep6915:fix-gh25390

Conversation

@chep6915

Copy link
Copy Markdown

Fixes GH-25390 (regression of the fix for GH-23443, #23462).

When the driver returns SQL_NO_TOTAL for a long column, a4c4173 started using the declared column size (SQL_DESC_DISPLAY_SIZE, C->datalen) 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 emalloc() that much and the process was killed by the memory limit (Hard memory limit ... tried to allocate 2147483648 bytes, exit 137).

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. This reads block by block (LONG_COLUMN_BUFFER_SIZE - 1) whenever the driver cannot tell the total length, which is what the code did before a4c4173 for large declared sizes; that path already covers the declared-size-0 case of GH-23443 (the block size is always > 0). The fix is the one suggested by @licvido in the issue; the regression test is new.

Verification

Built PHP-8.5 with and without this change (macOS arm64, unixODBC 2.3, ODBC Driver 18 for SQL Server, SQL Server 2022), fetching literal values with memory_limit=256M, each case in its own process:

Case without with
ntext 5000 chars, 100 000 multi-byte chars killed, tries to allocate 1 GiB ok, peak 2 MB
text 4073 / 300 000 / 3 000 000 bytes ok (this driver reports the total for text; FreeTDS does not, see the issue) ok, 3 MB in 3 s instead of 10 s
varchar(max) / nvarchar(max) 4499 (GH-23443), 5000, 300 000 ok ok
block boundaries ntext 4063 / 4064 / 8126 / 8127, ntext 3 000 000 killed ok, length and md5 as expected
NULL text / ntext / nvarchar(max), NULL after a long column, two text columns in one row, varchar(8000), image 3 MB ok ok

ext/pdo_odbc/tests against the same server: 142 passed, 0 failed, 18 skipped (need the SQLite ODBC driver). The new gh25390.phpt fails without the change (Termsig=9, 1 GiB allocation) and passes with it.

🤖 Generated with Claude Code

… text columns

When the driver returns SQL_NO_TOTAL for a long column, the fix for phpGH-23443
(a4c4173) 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 a4c4173 for large declared sizes;
that path already handles the declared size 0 case of phpGH-23443.

Fix suggested by licvido in the issue.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant