Skip to content

Fix undefined behavior in php_stream_memory_seek() - #21353

Closed
lacatoire wants to merge 1 commit into
php:masterfrom
lacatoire:fix/ubsan-memory-stream-seek-upstream
Closed

Fix undefined behavior in php_stream_memory_seek()#21353
lacatoire wants to merge 1 commit into
php:masterfrom
lacatoire:fix/ubsan-memory-stream-seek-upstream

Conversation

@lacatoire

Copy link
Copy Markdown
Member

Fix signed integer overflow when negating offset in php_stream_memory_seek(). When offset == ZEND_LONG_MIN, (size_t)(-offset) is undefined behavior. Uses (size_t)0 - (size_t)offset to perform the negation in unsigned arithmetic.

When offset is ZEND_LONG_MIN, (size_t)(-offset) triggers signed
integer overflow which is undefined behavior. Replace with
(size_t)0 - (size_t)offset to perform the negation in unsigned
arithmetic, which is well-defined.
Also adds a test to verify that seeking with PHP_INT_MIN does not
crash and that the stream remains usable afterwards.
@iluuu1994

Copy link
Copy Markdown
Member

Hi @lacatoire. There's already a PR open for this: GH-20965. Though it seems Jakub (who generally handles stream stuff) is busy atm.

@lacatoire
lacatoire deleted the fix/ubsan-memory-stream-seek-upstream branch April 30, 2026 09:05
Sign up for freeto 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

@lacatoire@iluuu1994