From: Antony Dovgal Date: Wed, 21 Jun 2006 14:38:06 +0000 (+0000) Subject: improve check for stream mode, add tests X-Git-Tag: RELEASE_1_0_0RC1~2671 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=06ae96342f7a10c34812dd4a5212757e2320a275;p=php improve check for stream mode, add tests --- diff --git a/ext/bz2/bz2.c b/ext/bz2/bz2.c index b0467fca88..84b58b7439 100644 --- a/ext/bz2/bz2.c +++ b/ext/bz2/bz2.c @@ -375,19 +375,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); + 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; + } - if (!memchr(stream->mode, Z_STRVAL_PP(mode)[0], strlen(stream->mode))) { - switch (Z_STRVAL_PP(mode)[0]) { - case 'r': + 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"); - break; - case 'w': + 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"); - break; - } - RETURN_FALSE; + RETURN_FALSE; + } + break; + default: + /* not reachable */ + break; } if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void *) &fd, REPORT_ERRORS)) { diff --git a/ext/bz2/tests/001.phpt b/ext/bz2/tests/001.phpt new file mode 100644 index 0000000000..0e1265f406 --- /dev/null +++ b/ext/bz2/tests/001.phpt @@ -0,0 +1,41 @@ +--TEST-- +bzopen() and invalid parameters +--FILE-- + +--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 diff --git a/ext/bz2/tests/002.phpt b/ext/bz2/tests/002.phpt new file mode 100644 index 0000000000..fc538d6024 --- /dev/null +++ b/ext/bz2/tests/002.phpt @@ -0,0 +1,127 @@ +--TEST-- +bzopen() using fd opened in wrong mode +--FILE-- + +--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