Skip to content

Fix buffer overflow converting @@IDENTITY in pdo_dblib lastInsertId#22348

Closed
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix-pdo-dblib-lastid-overflow
Closed

Fix buffer overflow converting @@IDENTITY in pdo_dblib lastInsertId#22348
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix-pdo-dblib-lastid-overflow

Conversation

@iliaal

@iliaal iliaal commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

dblib_handle_last_id() converts @@IDENTITY into a 32-byte buffer with dbconvert()'s destination length set to -1, which disables FreeTDS's destination bounds check. A numeric(p,0) IDENTITY column with precision above 30 produces a textual form longer than 32 bytes (31 digits plus a sign plus the NUL), overflowing the buffer.

Size the buffer for the widest @@IDENTITY (numeric(38,0) = 38 digits + sign + NUL = 40 bytes) and pass the real destination length, mirroring the existing fix in pdo_dblib_stmt_stringify_col().

dblib_handle_last_id() converted the @@IDENTITY value into a 32-byte
buffer with dbconvert()'s destination length set to -1, which disables
FreeTDS's destination bounds check. A numeric(p,0) IDENTITY column with
precision over ~30 produces a textual form longer than 32 bytes,
overflowing the buffer. Size the buffer for the widest @@IDENTITY
(numeric(38,0): 38 digits, sign, NUL) and pass the real destination
length so dbconvert() stays in bounds, mirroring the explicit-destlen
fix already in pdo_dblib_stmt_stringify_col().
@iliaal iliaal closed this in 1a5a81c Jun 17, 2026
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