Skip to content

Commit

Permalink
Fix stubs for bzerr* functions
Browse files Browse the repository at this point in the history
These can return false if the stream is not a bz2 stream. Also
emit a warning in this case, this should not be failing silently.
  • Loading branch information
nikic committed Nov 5, 2021
1 parent b302bfa commit 9b2a3c1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions ext/bz2/bz2.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int opt)
php_stream_from_zval(stream, bzp);

if (!php_stream_is(stream, PHP_STREAM_IS_BZIP2)) {
php_error_docref(NULL, E_WARNING, "Stream is not a bz2 stream");
RETURN_FALSE;
}

Expand Down
6 changes: 3 additions & 3 deletions ext/bz2/bz2.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ function bzflush($bz): bool {}
function bzclose($bz): bool {}

/** @param resource $bz */
function bzerrno($bz): int {}
function bzerrno($bz): int|false {}

/** @param resource $bz */
function bzerrstr($bz): string {}
function bzerrstr($bz): string|false {}

/** @param resource $bz */
function bzerror($bz): array {}
function bzerror($bz): array|false {}

function bzcompress(string $data, int $block_size = 4, int $work_factor = 0): string|int {}

Expand Down
8 changes: 4 additions & 4 deletions ext/bz2/bz2_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 0cd7792480671883ebae30ae8358b8f8e3390474 */
* Stub hash: 67689133cbaffce7c139578f5070ecfc8ab6ec55 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_bzopen, 0, 0, 2)
ZEND_ARG_INFO(0, file)
Expand All @@ -23,15 +23,15 @@ ZEND_END_ARG_INFO()

#define arginfo_bzclose arginfo_bzflush

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bzerrno, 0, 1, IS_LONG, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bzerrno, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_INFO(0, bz)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bzerrstr, 0, 1, IS_STRING, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bzerrstr, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_INFO(0, bz)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bzerror, 0, 1, IS_ARRAY, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bzerror, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_ARG_INFO(0, bz)
ZEND_END_ARG_INFO()

Expand Down
20 changes: 20 additions & 0 deletions ext/bz2/tests/bzerr_functions_on_invalid_stream.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Calling bzerr* functions on non-bz2 streams
--SKIPIF--
<?php if (!extension_loaded("bz2")) print "skip"; ?>
--FILE--
<?php
$f = fopen(__FILE__, 'r');
var_dump(bzerrno($f));
var_dump(bzerrstr($f));
var_dump(bzerror($f));
?>
--EXPECTF--
Warning: bzerrno(): Stream is not a bz2 stream in %s on line %d
bool(false)

Warning: bzerrstr(): Stream is not a bz2 stream in %s on line %d
bool(false)

Warning: bzerror(): Stream is not a bz2 stream in %s on line %d
bool(false)

0 comments on commit 9b2a3c1

Please sign in to comment.