Skip to content

Fix bzopen() ownership of the stream it wraps - #23824

Open
bukka wants to merge 1 commit into
php:PHP-8.4from
bukka:bz2_bzdopen_dup
Open

bukka wants to merge 1 commit into
php:PHP-8.4from
bukka:bz2_bzdopen_dup

Conversation

@bukka

@bukka bukka commented Sep 21, 2026

Copy link
Copy Markdown
Member

bzopen() with a stream argument, and the wrapper fallback in _php_stream_bz2open(), pass the stream's own descriptor to BZ2_bzdopen(). bzlib takes ownership of it and closes it in BZ2_bzclose(), after which the inner stream close closes the same descriptor number again. The bz2 stream also keeps a raw pointer to the inner stream while holding a reference on its resource only, so closing the inner stream with fclose() left a dangling pointer that the bz2 close dereferenced, while bzlib kept using a descriptor number that could already belong to another file.

Hand bzlib a duplicate of the descriptor and resolve the inner stream from its resource on close, so a closed inner stream is skipped.

@bukka
bukka changed the base branch from master to PHP-8.3 September 21, 2026 14:28
@bukka
bukka changed the base branch from PHP-8.3 to master September 21, 2026 14:28
bzopen() with a stream argument, and the wrapper fallback in
_php_stream_bz2open(), pass the stream's own descriptor to
BZ2_bzdopen(). bzlib takes ownership of it and closes it in
BZ2_bzclose(), after which the inner stream close closes the same
descriptor number again. The bz2 stream also keeps a raw pointer to the
inner stream while holding a reference on its resource only, so closing
the inner stream with fclose() left a dangling pointer that the bz2
close dereferenced, while bzlib kept using a descriptor number that
could already belong to another file.

Hand bzlib a duplicate of the descriptor and resolve the inner stream
from its resource on close, so a closed inner stream is skipped.
@bukka
bukka changed the base branch from master to PHP-8.4 September 21, 2026 14:34
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