From b6708237493336afc87dd4afd56a887e367ec55b Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Sun, 8 Dec 2019 13:30:46 +0100 Subject: [PATCH] Convert some warnings to Errors in BZip2 Closes GH-4984 --- ext/bz2/bz2.c | 14 ++++++------- ext/bz2/tests/001.phpt | 43 +++++++++++++++++++++++++-------------- ext/bz2/tests/002.phpt | 22 +++++++++++--------- ext/bz2/tests/003-mb.phpt | 16 ++++++++------- ext/bz2/tests/003.phpt | 16 ++++++++------- 5 files changed, 65 insertions(+), 46 deletions(-) diff --git a/ext/bz2/bz2.c b/ext/bz2/bz2.c index 9b84feafe1..d6cdcacdeb 100644 --- a/ext/bz2/bz2.c +++ b/ext/bz2/bz2.c @@ -338,9 +338,9 @@ static PHP_FUNCTION(bzread) php_stream_from_zval(stream, bz); - if ((len + 1) < 1) { - php_error_docref(NULL, E_WARNING, "length may not be negative"); - RETURN_FALSE; + if (len < 0) { + zend_value_error("Length cannot be negative"); + RETURN_THROWS(); } data = php_stream_read_to_str(stream, len); @@ -367,8 +367,8 @@ static PHP_FUNCTION(bzopen) } if (mode_len != 1 || (mode[0] != 'r' && mode[0] != 'w')) { - php_error_docref(NULL, E_WARNING, "'%s' is not a valid mode for bzopen(). Only 'w' and 'r' are supported.", mode); - RETURN_FALSE; + zend_value_error("'%s' is not a valid mode for bzopen(). Only 'w' and 'r' are supported.", mode); + RETURN_THROWS(); } /* If it's not a resource its a string containing the filename to open */ @@ -430,8 +430,8 @@ static PHP_FUNCTION(bzopen) stream = php_stream_bz2open_from_BZFILE(bz, mode, stream); } else { - php_error_docref(NULL, E_WARNING, "first parameter has to be string or file-resource"); - RETURN_FALSE; + zend_type_error("First parameter has to be string or file-resource"); + RETURN_THROWS(); } if (stream) { diff --git a/ext/bz2/tests/001.phpt b/ext/bz2/tests/001.phpt index ee1872f0ef..a0e8d2e60e 100644 --- a/ext/bz2/tests/001.phpt +++ b/ext/bz2/tests/001.phpt @@ -5,35 +5,48 @@ bzopen() and invalid parameters --FILE-- getMessage() . \PHP_EOL; +} + + var_dump(bzopen("", "r")); var_dump(bzopen("", "w")); -var_dump(bzopen("", "x")); -var_dump(bzopen("", "rw")); -var_dump(bzopen("no_such_file", "r")); + +try { + var_dump(bzopen("", "x")); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} + +try { + var_dump(bzopen("", "rw")); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} + +try { + var_dump(bzopen("no_such_file", "r")); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} $fp = fopen(__FILE__,"r"); var_dump(bzopen($fp, "r")); -echo "Done\n"; ?> --EXPECTF-- -Warning: bzopen(): '' is not a valid mode for bzopen(). Only 'w' and 'r' are supported. in %s on line %d -bool(false) +'' is not a valid mode for bzopen(). Only 'w' and 'r' are supported. Warning: bzopen(): filename cannot be empty in %s on line %d bool(false) Warning: bzopen(): filename cannot be empty in %s on line %d bool(false) - -Warning: bzopen(): 'x' is not a valid mode for bzopen(). Only 'w' and 'r' are supported. in %s on line %d -bool(false) - -Warning: bzopen(): 'rw' is not a valid mode for bzopen(). Only 'w' and 'r' are supported. in %s on line %d -bool(false) +'x' is not a valid mode for bzopen(). Only 'w' and 'r' are supported. +'rw' is not a valid mode for bzopen(). Only 'w' and 'r' are supported. Warning: bzopen(no_such_file): failed to open stream: No such file or directory in %s on line %d bool(false) resource(%d) of type (stream) -Done diff --git a/ext/bz2/tests/002.phpt b/ext/bz2/tests/002.phpt index a69514a711..c8069cc4a4 100644 --- a/ext/bz2/tests/002.phpt +++ b/ext/bz2/tests/002.phpt @@ -28,10 +28,18 @@ $fp = fopen("bz_open_002.txt", "wb"); var_dump(bzopen($fp, "w")); $fp = fopen("bz_open_002.txt", "br"); -var_dump(bzopen($fp, "r")); +try { + var_dump(bzopen($fp, "r")); +} catch (\TypeError $e) { + echo $e->getMessage() . \PHP_EOL; +} $fp = fopen("bz_open_002.txt", "br"); -var_dump(bzopen($fp, "w")); +try { + var_dump(bzopen($fp, "w")); +} catch (\TypeError $e) { + echo $e->getMessage() . \PHP_EOL; +} $fp = fopen("bz_open_002.txt", "r"); var_dump(bzopen($fp, "w")); @@ -71,7 +79,6 @@ var_dump(bzopen($fp, "w")); @unlink("bz_open_002.txt"); -echo "Done\n"; ?> --EXPECTF-- resource(%d) of type (stream) @@ -84,14 +91,10 @@ resource(%d) of type (stream) resource(%d) of type (stream) Warning: fopen(bz_open_002.txt): failed to open stream: Bad file %s in %s on line %d - -Warning: bzopen(): first parameter has to be string or file-resource in %s on line %d -bool(false) +First parameter has to be string or file-resource Warning: fopen(bz_open_002.txt): failed to open stream: Bad file %s in %s on line %d - -Warning: bzopen(): first parameter has to be string or file-resource in %s on line %d -bool(false) +First parameter has to be string or file-resource Warning: bzopen(): cannot write to a stream opened in read only mode in %s on line %d bool(false) @@ -126,4 +129,3 @@ bool(false) Warning: bzopen(): cannot read from a stream opened in write only mode in %s on line %d bool(false) resource(%d) of type (stream) -Done diff --git a/ext/bz2/tests/003-mb.phpt b/ext/bz2/tests/003-mb.phpt index b5f8fe814e..c01a3d18c8 100644 --- a/ext/bz2/tests/003-mb.phpt +++ b/ext/bz2/tests/003-mb.phpt @@ -7,18 +7,21 @@ bzread() tests $fd = bzopen(__DIR__."/003私はガラスを食べられます.txt.bz2","r"); var_dump(bzread($fd, 0)); -var_dump(bzread($fd, -10)); + +try { + var_dump(bzread($fd, -10)); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} + var_dump(bzread($fd, 1)); var_dump(bzread($fd, 2)); var_dump(bzread($fd, 100000)); -echo "Done\n"; ?> ---EXPECTF-- +--EXPECT-- string(0) "" - -Warning: bzread(): length may not be negative in %s on line %d -bool(false) +Length cannot be negative string(1) "R" string(2) "is" string(251) "ing up from the heart of the desert @@ -30,4 +33,3 @@ Rising up for Jerusalem Rising up from the heat of the desert Heading out for Jerusalem " -Done diff --git a/ext/bz2/tests/003.phpt b/ext/bz2/tests/003.phpt index cc5b2a3787..6c1f8ea4a9 100644 --- a/ext/bz2/tests/003.phpt +++ b/ext/bz2/tests/003.phpt @@ -7,18 +7,21 @@ bzread() tests $fd = bzopen(__DIR__."/003.txt.bz2","r"); var_dump(bzread($fd, 0)); -var_dump(bzread($fd, -10)); + +try { + var_dump(bzread($fd, -10)); +} catch (\ValueError $e) { + echo $e->getMessage() . \PHP_EOL; +} + var_dump(bzread($fd, 1)); var_dump(bzread($fd, 2)); var_dump(bzread($fd, 100000)); -echo "Done\n"; ?> ---EXPECTF-- +--EXPECT-- string(0) "" - -Warning: bzread(): length may not be negative in %s on line %d -bool(false) +Length cannot be negative string(1) "R" string(2) "is" string(251) "ing up from the heart of the desert @@ -30,4 +33,3 @@ Rising up for Jerusalem Rising up from the heat of the desert Heading out for Jerusalem " -Done -- 2.40.0