mirror of
https://github.com/php/php-src.git
synced 2024-09-21 18:07:23 +00:00
MFH: improve check for stream mode, add tests
This commit is contained in:
parent
8c30a7f8a6
commit
4b674fb7b0
@ -381,19 +381,39 @@ PHP_FUNCTION(bzopen)
|
||||
} else {
|
||||
/* If it is a resource, than its a stream resource */
|
||||
int fd;
|
||||
int stream_mode_len;
|
||||
|
||||
php_stream_from_zval(stream, file);
|
||||
|
||||
if (!memchr(stream->mode, Z_STRVAL_PP(mode)[0], strlen(stream->mode))) {
|
||||
switch (Z_STRVAL_PP(mode)[0]) {
|
||||
case 'r':
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot read from a stream opened in write only mode");
|
||||
break;
|
||||
case 'w':
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot write to a stream opened in read only mode");
|
||||
break;
|
||||
}
|
||||
stream_mode_len = strlen(stream->mode);
|
||||
|
||||
if (stream_mode_len != 1 && !(stream_mode_len == 2 && memchr(stream->mode, 'b', 2))) {
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
|
||||
RETURN_FALSE;
|
||||
} else if (stream_mode_len == 1 && stream->mode[0] != 'r' && stream->mode[0] != 'w' && stream->mode[0] != 'a' && stream->mode[0] != 'x') {
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
||||
switch(Z_STRVAL_PP(mode)[0]) {
|
||||
case 'r':
|
||||
/* only "r" and "rb" are supported */
|
||||
if (stream->mode[0] != Z_STRVAL_PP(mode)[0] && !(stream_mode_len == 2 && stream->mode[1] != Z_STRVAL_PP(mode)[0])) {
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot read from a stream opened in write only mode");
|
||||
RETURN_FALSE;
|
||||
}
|
||||
break;
|
||||
case 'w':
|
||||
/* support only "w"(b), "a"(b), "x"(b) */
|
||||
if (stream->mode[0] != Z_STRVAL_PP(mode)[0] && !(stream_mode_len == 2 && stream->mode[1] != Z_STRVAL_PP(mode)[0])
|
||||
&& stream->mode[0] != 'a' && !(stream_mode_len == 2 && stream->mode[1] != 'a')
|
||||
&& stream->mode[0] != 'x' && !(stream_mode_len == 2 && stream->mode[1] != 'x')) {
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot write to a stream opened in read only mode");
|
||||
RETURN_FALSE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/* not reachable */
|
||||
break;
|
||||
}
|
||||
|
||||
if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void *) &fd, REPORT_ERRORS)) {
|
||||
|
41
ext/bz2/tests/001.phpt
Normal file
41
ext/bz2/tests/001.phpt
Normal file
@ -0,0 +1,41 @@
|
||||
--TEST--
|
||||
bzopen() and invalid parameters
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
var_dump(bzopen());
|
||||
var_dump(bzopen("", ""));
|
||||
var_dump(bzopen("", "r"));
|
||||
var_dump(bzopen("", "w"));
|
||||
var_dump(bzopen("", "x"));
|
||||
var_dump(bzopen("", "rw"));
|
||||
var_dump(bzopen("no_such_file", "r"));
|
||||
|
||||
$fp = fopen(__FILE__,"r");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
echo "Done\n";
|
||||
?>
|
||||
--EXPECTF--
|
||||
Warning: Wrong parameter count for bzopen() in %s on line %d
|
||||
NULL
|
||||
|
||||
Warning: bzopen(): '' is not a valid mode for bzopen(). Only 'w' and 'r' are supported. in %s on line %d
|
||||
bool(false)
|
||||
|
||||
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)
|
||||
|
||||
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
|
127
ext/bz2/tests/002.phpt
Normal file
127
ext/bz2/tests/002.phpt
Normal file
@ -0,0 +1,127 @@
|
||||
--TEST--
|
||||
bzopen() using fd opened in wrong mode
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
@unlink("bz_open_002.txt");
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "w");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "r");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
@unlink("bz_open_002.txt");
|
||||
$fp = fopen("bz_open_002.txt", "x");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
@unlink("bz_open_002.txt");
|
||||
$fp = fopen("bz_open_002.txt", "x");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "rb");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "wb");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "br");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "br");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "r");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "w");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "rw");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "rw");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "wr");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "wr");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "r+");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "r+");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "w+");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "w+");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "a");
|
||||
var_dump(bzopen($fp, "r"));
|
||||
|
||||
$fp = fopen("bz_open_002.txt", "a");
|
||||
var_dump(bzopen($fp, "w"));
|
||||
|
||||
@unlink("bz_open_002.txt");
|
||||
|
||||
echo "Done\n";
|
||||
?>
|
||||
--EXPECTF--
|
||||
resource(%d) of type (stream)
|
||||
resource(%d) of type (stream)
|
||||
resource(%d) of type (stream)
|
||||
|
||||
Warning: bzopen(): cannot read from a stream opened in write only mode in %s on line %d
|
||||
bool(false)
|
||||
resource(%d) of type (stream)
|
||||
resource(%d) of type (stream)
|
||||
|
||||
Warning: fopen(bz_open_002.txt): failed to open stream: Bad file descriptor in %s on line %d
|
||||
|
||||
Warning: bzopen(): filename cannot be empty in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: fopen(bz_open_002.txt): failed to open stream: Bad file descriptor in %s on line %d
|
||||
|
||||
Warning: bzopen(): filename cannot be empty in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot write to a stream opened in read only mode in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot read from a stream opened in write only mode in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot use stream opened in mode 'rw' in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot use stream opened in mode 'rw' in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot use stream opened in mode 'wr' in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot use stream opened in mode 'wr' in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot use stream opened in mode 'r+' in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot use stream opened in mode 'r+' in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot use stream opened in mode 'w+' in %s on line %d
|
||||
bool(false)
|
||||
|
||||
Warning: bzopen(): cannot use stream opened in mode 'w+' in %s on line %d
|
||||
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
|
Loading…
Reference in New Issue
Block a user