Skip to content

Convert some warnings to Errors in BZip2 #4984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ext/bz2/bz2.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ 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;
zend_value_error("length cannot be negative");
RETURN_THROWS();;
}

data = php_stream_read_to_str(stream, len);
Expand All @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down
43 changes: 28 additions & 15 deletions ext/bz2/tests/001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,48 @@ bzopen() and invalid parameters
--FILE--
<?php

var_dump(bzopen("", ""));
var_dump(bzopen("", "r"));
try {
var_dump(bzopen("", ""));
} catch (\ValueError $e) {
echo $e->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
22 changes: 12 additions & 10 deletions ext/bz2/tests/002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -71,7 +79,6 @@ var_dump(bzopen($fp, "w"));

@unlink("bz_open_002.txt");

echo "Done\n";
?>
--EXPECTF--
resource(%d) of type (stream)
Expand All @@ -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)
Expand Down Expand Up @@ -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
16 changes: 9 additions & 7 deletions ext/bz2/tests/003-mb.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,4 +33,3 @@ Rising up for Jerusalem
Rising up from the heat of the desert
Heading out for Jerusalem
"
Done
16 changes: 9 additions & 7 deletions ext/bz2/tests/003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,4 +33,3 @@ Rising up for Jerusalem
Rising up from the heat of the desert
Heading out for Jerusalem
"
Done