Skip to content

Fix undefined behavior in php_stream_memory_seek()#21353

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

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

Conversation

@lacatoire
Copy link
Contributor

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
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 closed this Mar 6, 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