diff options
author | Antony Dovgal <tony2001@php.net> | 2006-06-21 14:38:28 +0000 |
---|---|---|
committer | Antony Dovgal <tony2001@php.net> | 2006-06-21 14:38:28 +0000 |
commit | 4b674fb7b04ceda931f8415c264d4f309ff17280 (patch) | |
tree | fc06aa691b38a9548cc691df852d707770fd4b1a /ext/bz2 | |
parent | 8c30a7f8a616729f263591bc19e6697724548868 (diff) | |
download | php-git-4b674fb7b04ceda931f8415c264d4f309ff17280.tar.gz |
MFH: improve check for stream mode, add tests
Diffstat (limited to 'ext/bz2')
-rw-r--r-- | ext/bz2/bz2.c | 36 | ||||
-rw-r--r-- | ext/bz2/tests/001.phpt | 41 | ||||
-rw-r--r-- | ext/bz2/tests/002.phpt | 127 |
3 files changed, 196 insertions, 8 deletions
diff --git a/ext/bz2/bz2.c b/ext/bz2/bz2.c index 9df6634ecd..eb9f4bc5ac 100644 --- a/ext/bz2/bz2.c +++ b/ext/bz2/bz2.c @@ -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); + 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-- +<?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 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-- +<?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 |