Skip to content

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

Open
chep6915 wants to merge 4 commits into
php:PHP-8.5from
chep6915:fix-gh25390
Open

chep6915 wants to merge 4 commits into
php:PHP-8.5from
chep6915:fix-gh25390

Conversation

@chep6915

@chep6915 chep6915 commented Nov 20, 2026 •

Copy link
Copy Markdown

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

When SQLGetData() returns SQL_NO_TOTAL for a long column,
the code introduced by the fix for GH-23443 used the declared column size as the buffer
size for the next read. For SQL Server text/ntext columns the declared
size is 2 GB/1 GB, so fetching any value larger than the first buffer
tried to allocate that much memory and hit the memory limit.

The declared size is only the maximum for the column type, not the
length of the value. When the driver does not report the total length,
read the data in blocks of LONG_COLUMN_BUFFER_SIZE - 1 bytes instead.
This is also correct for the declared size 0 case from GH-23443.

Fix suggested by licvido in the issue.

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>
@chep6915

chep6915 commented Dec 1, 2026

Copy link
Copy Markdown
Author

@NattyNarwhal @SakiTakamachi Friendly ping — this fixes a regression (GH-25390) from the GH-23443 fix. CI is green and a regression test is included. Happy to adjust anything if needed. Thanks!

@NattyNarwhal NattyNarwhal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct code wise, but the prose on the commit message made it hard to understand at first. Stepping through it in a debugger made it clear.

Comment thread NEWS Resolved
Comment thread ext/pdo_odbc/odbc_stmt.c Outdated
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the original comment, the new one repeats the larger comment above and the historical context is not essential here

@chep6915

chep6915 commented Dec 3, 2026

Copy link
Copy Markdown
Author

@NattyNarwhal
Thanks for the review! I've restored the original comment, fixed the NEWS spacing, and rewrote the PR description to make the explanation clearer.

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.

2 participants