From bd55197721bee8003a46d946f8e601eec6c42144 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 22 Feb 2003 02:43:58 +0000 Subject: [PATCH] Miscellaneous streams fixes, including probable fixes for: Bug #21131 (fopen with 'a+' and rewind() doesn't work) Bug #21713 (include remote files leaks temporary files + descriptors on Solaris) Bug #21185 (move_uploaded_file() does not ignore open_basedir as it should) Bug #22362 (combinations of fwrite(), fread() and fseek() produce unexpected results) --- ext/standard/file.c | 2 +- ext/standard/tests/file/bug21131.phpt | 5 ----- ext/standard/tests/file/bug22362.phpt | 31 +++++++++++++++++++++++++++ main/php_streams.h | 5 +++++ main/streams.c | 31 +++++++++++++++++---------- 5 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 ext/standard/tests/file/bug22362.phpt diff --git a/ext/standard/file.c b/ext/standard/file.c index fbb5a3297a..e565f9f00c 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -2038,7 +2038,7 @@ PHPAPI int php_copy_file(char *src, char *dest TSRMLS_DC) int ret = FAILURE; srcstream = php_stream_open_wrapper(src, "rb", - ENFORCE_SAFE_MODE | REPORT_ERRORS, + STREAM_DISABLE_OPEN_BASEDIR | REPORT_ERRORS, NULL); if (!srcstream) diff --git a/ext/standard/tests/file/bug21131.phpt b/ext/standard/tests/file/bug21131.phpt index 00c3781f9c..7950f9fd0d 100644 --- a/ext/standard/tests/file/bug21131.phpt +++ b/ext/standard/tests/file/bug21131.phpt @@ -9,15 +9,10 @@ fwrite($fp, "foobar"); fclose($fp); $fp = fopen($filename, "a+"); -var_dump(ftell($fp)); rewind($fp); -var_dump(ftell($fp)); fpassthru($fp); fclose($fp); unlink($filename); ?> --EXPECT-- -int(6) -int(0) foobar - diff --git a/ext/standard/tests/file/bug22362.phpt b/ext/standard/tests/file/bug22362.phpt new file mode 100644 index 0000000000..ce88bf3a07 --- /dev/null +++ b/ext/standard/tests/file/bug22362.phpt @@ -0,0 +1,31 @@ +--TEST-- +Bug #22362: combinations of fseek() and fwrite() produce unexpected results. +--FILE-- + +--EXPECT-- +string(6) "quxb!r" +string(6) "barf!o" diff --git a/main/php_streams.h b/main/php_streams.h index c506b060f2..4033df6c51 100755 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -502,6 +502,11 @@ PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show /* this flag is only used by include/require functions */ #define STREAM_OPEN_FOR_INCLUDE 128 +/* 512 skipped for PHP 5 compat */ + +/* if set, skip open_basedir checks */ +#define STREAM_DISABLE_OPEN_BASEDIR 1024 + /* Antique - no longer has meaning */ #define IGNORE_URL_WIN 0 diff --git a/main/streams.c b/main/streams.c index 4f8454989d..4558ed8663 100755 --- a/main/streams.c +++ b/main/streams.c @@ -852,6 +852,15 @@ PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t coun if (buf == NULL || count == 0 || stream->ops->write == NULL) return 0; + /* if we have a seekable stream we need to ensure that data is written at the + * current stream->position. This means invalidating the read buffer and then + * performing a low-level seek */ + if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) { + stream->readpos = stream->writepos = 0; + + stream->ops->seek(stream, stream->position, SEEK_SET, &stream->position TSRMLS_CC); + } + while (count > 0) { towrite = count; if (towrite > stream->chunk_size) @@ -871,8 +880,6 @@ PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t coun * buffered from fifos and sockets */ if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) { stream->position += justwrote; - stream->writepos = 0; - stream->readpos = 0; } } else { break; @@ -907,10 +914,6 @@ PHPAPI off_t _php_stream_tell(php_stream *stream TSRMLS_DC) PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC) { - /* not moving anywhere */ - if ((offset == 0 && whence == SEEK_CUR) || (offset == stream->position && whence == SEEK_SET)) - return 0; - /* handle the case where we are in the buffer */ if ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) == 0) { switch(whence) { @@ -933,9 +936,6 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_ break; } } - - /* invalidate the buffer contents */ - stream->readpos = stream->writepos = 0; if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) { int ret; @@ -954,6 +954,10 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_ if (((stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) || ret == 0) { if (ret == 0) stream->eof = 0; + + /* invalidate the buffer contents */ + stream->readpos = stream->writepos = 0; + return ret; } /* else the stream has decided that it can't support seeking after all; @@ -1996,7 +2000,12 @@ PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show return FAILURE; #endif - if (flags & PHP_STREAM_CAST_TRY_HARD) { + if (!stream->filterhead && stream->ops->cast && stream->ops->cast(stream, castas, NULL TSRMLS_CC) == SUCCESS) { + if (FAILURE == stream->ops->cast(stream, castas, ret TSRMLS_CC)) { + return FAILURE; + } + goto exit_success; + } else if (flags & PHP_STREAM_CAST_TRY_HARD) { php_stream *newstream; newstream = php_stream_fopen_tmpfile(); @@ -2195,7 +2204,7 @@ static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, ch return php_stream_fopen_with_path_rel(path, mode, PG(include_path), opened_path, options); } - if (php_check_open_basedir(path TSRMLS_CC)) { + if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(path TSRMLS_CC)) { return NULL; } -- 2.40.0