lbarnaud Sun Apr 19 13:50:25 2009 UTC Modified files: /php-src/ext/standard file.c streamsfuncs.c /php-src/main php_streams.h /php-src/main/streams cast.c streams.c Log: MFB5.3: Fixed bug #47997 (stream_copy_to_stream returns 1 on empty streams)
http://cvs.php.net/viewvc.cgi/php-src/ext/standard/file.c?r1=1.540&r2=1.541&diff_format=u Index: php-src/ext/standard/file.c diff -u php-src/ext/standard/file.c:1.540 php-src/ext/standard/file.c:1.541 --- php-src/ext/standard/file.c:1.540 Thu Mar 26 20:02:28 2009 +++ php-src/ext/standard/file.c Sun Apr 19 13:50:24 2009 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.540 2009/03/26 20:02:28 felipe Exp $ */ +/* $Id: file.c,v 1.541 2009/04/19 13:50:24 lbarnaud Exp $ */ /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */ @@ -662,7 +662,10 @@ switch (Z_TYPE_P(data)) { case IS_RESOURCE: - numchars = php_stream_copy_to_stream(srcstream, stream, PHP_STREAM_COPY_ALL); + numchars = (int) php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL); + if ((size_t)numchars == PHP_STREAM_FAILURE) { + numchars = -1; + } break; case IS_ARRAY: if (zend_hash_num_elements(Z_ARRVAL_P(data))) { @@ -1949,7 +1952,7 @@ deststream = php_stream_open_wrapper(dest, "wb", REPORT_ERRORS, NULL); if (srcstream && deststream) { - ret = php_stream_copy_to_stream(srcstream, deststream, PHP_STREAM_COPY_ALL) == 0 ? FAILURE : SUCCESS; + ret = php_stream_copy_to_stream_ex(srcstream, deststream, PHP_STREAM_COPY_ALL) == PHP_STREAM_FAILURE ? FAILURE : SUCCESS; } if (srcstream) { php_stream_close(srcstream); http://cvs.php.net/viewvc.cgi/php-src/ext/standard/streamsfuncs.c?r1=1.136&r2=1.137&diff_format=u Index: php-src/ext/standard/streamsfuncs.c diff -u php-src/ext/standard/streamsfuncs.c:1.136 php-src/ext/standard/streamsfuncs.c:1.137 --- php-src/ext/standard/streamsfuncs.c:1.136 Thu Mar 26 20:02:29 2009 +++ php-src/ext/standard/streamsfuncs.c Sun Apr 19 13:50:24 2009 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: streamsfuncs.c,v 1.136 2009/03/26 20:02:29 felipe Exp $ */ +/* $Id: streamsfuncs.c,v 1.137 2009/04/19 13:50:24 lbarnaud Exp $ */ #include "php.h" #include "php_globals.h" @@ -458,6 +458,7 @@ php_stream *src, *dest; zval *zsrc, *zdest; long maxlen = PHP_STREAM_COPY_ALL, pos = 0; + size_t ret; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|ll", &zsrc, &zdest, &maxlen, &pos) == FAILURE) { RETURN_FALSE; @@ -471,7 +472,12 @@ RETURN_FALSE; } - RETURN_LONG(php_stream_copy_to_stream(src, dest, maxlen)); + ret = php_stream_copy_to_stream_ex(src, dest, maxlen); + + if (ret == PHP_STREAM_FAILURE) { + RETURN_FALSE; + } + RETURN_LONG(ret); } /* }}} */ http://cvs.php.net/viewvc.cgi/php-src/main/php_streams.h?r1=1.140&r2=1.141&diff_format=u Index: php-src/main/php_streams.h diff -u php-src/main/php_streams.h:1.140 php-src/main/php_streams.h:1.141 --- php-src/main/php_streams.h:1.140 Tue Mar 10 23:39:53 2009 +++ php-src/main/php_streams.h Sun Apr 19 13:50:25 2009 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_streams.h,v 1.140 2009/03/10 23:39:53 helly Exp $ */ +/* $Id: php_streams.h,v 1.141 2009/04/19 13:50:25 lbarnaud Exp $ */ #ifndef PHP_STREAMS_H #define PHP_STREAMS_H @@ -525,14 +525,25 @@ * Uses mmap if the src is a plain file and at offset 0 */ #define PHP_STREAM_COPY_ALL ((size_t)-1) +#define PHP_STREAM_FAILURE ((size_t)-1) + BEGIN_EXTERN_C() +ZEND_ATTRIBUTE_DEPRECATED PHPAPI size_t _php_stream_ucopy_to_stream(php_stream *src, php_stream *dest, size_t maxlen, size_t maxchars STREAMS_DC TSRMLS_DC); +ZEND_ATTRIBUTE_DEPRECATED PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC); /* Preserve "characters" semantics by having maxlen refer to maxchars in a unicode context */ #define php_stream_copy_to_stream(src, dest, maxlen) ( ((src)->readbuf_type == IS_STRING) \ ? _php_stream_copy_to_stream((src), (dest), (maxlen) STREAMS_CC TSRMLS_CC) \ : _php_stream_ucopy_to_stream((src), (dest), -1, (maxlen) STREAMS_CC TSRMLS_CC) ) +PHPAPI size_t _php_stream_ucopy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen, size_t maxchars STREAMS_DC TSRMLS_DC); +PHPAPI size_t _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC); +/* Preserve "characters" semantics by having maxlen refer to maxchars in a unicode context */ +#define php_stream_copy_to_stream_ex(src, dest, maxlen) ( ((src)->readbuf_type == IS_STRING) \ + ? _php_stream_copy_to_stream_ex((src), (dest), (maxlen) STREAMS_CC TSRMLS_CC) \ + : _php_stream_ucopy_to_stream_ex((src), (dest), -1, (maxlen) STREAMS_CC TSRMLS_CC) ) + /* read all data from stream and put into a buffer. Caller must free buffer when done. * The copy will use mmap if available. */ PHPAPI size_t _php_stream_copy_to_mem_ex(php_stream *src, zend_uchar rettype, void **buf, size_t maxlen, size_t maxchars, http://cvs.php.net/viewvc.cgi/php-src/main/streams/cast.c?r1=1.20&r2=1.21&diff_format=u Index: php-src/main/streams/cast.c diff -u php-src/main/streams/cast.c:1.20 php-src/main/streams/cast.c:1.21 --- php-src/main/streams/cast.c:1.20 Tue Mar 10 23:40:00 2009 +++ php-src/main/streams/cast.c Sun Apr 19 13:50:25 2009 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: cast.c,v 1.20 2009/03/10 23:40:00 helly Exp $ */ +/* $Id: cast.c,v 1.21 2009/04/19 13:50:25 lbarnaud Exp $ */ #define _GNU_SOURCE #include "php.h" @@ -214,9 +214,9 @@ newstream = php_stream_fopen_tmpfile(); if (newstream) { - size_t copied = php_stream_copy_to_stream(stream, newstream, PHP_STREAM_COPY_ALL); + size_t copied = php_stream_copy_to_stream_ex(stream, newstream, PHP_STREAM_COPY_ALL); - if (copied == 0) { + if (copied == PHP_STREAM_FAILURE) { php_stream_close(newstream); } else { int retcode = php_stream_cast(newstream, castas | flags, ret, show_err); @@ -332,7 +332,7 @@ (*newstream)->open_lineno = origstream->open_lineno; #endif - if (php_stream_copy_to_stream(origstream, *newstream, PHP_STREAM_COPY_ALL) == 0) { + if (php_stream_copy_to_stream_ex(origstream, *newstream, PHP_STREAM_COPY_ALL) == PHP_STREAM_FAILURE) { php_stream_close(*newstream); *newstream = NULL; return PHP_STREAM_CRITICAL; http://cvs.php.net/viewvc.cgi/php-src/main/streams/streams.c?r1=1.181&r2=1.182&diff_format=u Index: php-src/main/streams/streams.c diff -u php-src/main/streams/streams.c:1.181 php-src/main/streams/streams.c:1.182 --- php-src/main/streams/streams.c:1.181 Thu Mar 26 20:02:53 2009 +++ php-src/main/streams/streams.c Sun Apr 19 13:50:25 2009 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: streams.c,v 1.181 2009/03/26 20:02:53 felipe Exp $ */ +/* $Id: streams.c,v 1.182 2009/04/19 13:50:25 lbarnaud Exp $ */ #define _GNU_SOURCE #include "php.h" @@ -1768,14 +1768,14 @@ } /* Designed for copying UChars (taking into account both maxlen and maxchars) */ -PHPAPI size_t _php_stream_ucopy_to_stream(php_stream *src, php_stream *dest, size_t maxlen, size_t maxchars STREAMS_DC TSRMLS_DC) +PHPAPI size_t _php_stream_ucopy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen, size_t maxchars STREAMS_DC TSRMLS_DC) { size_t haveread = 0; php_stream_statbuf ssbuf; if (src->readbuf_type == IS_STRING) { /* Called incorrectly, don't do that. */ - return _php_stream_copy_to_stream(src, dest, maxlen STREAMS_CC TSRMLS_CC); + return _php_stream_copy_to_stream_ex(src, dest, maxlen STREAMS_CC TSRMLS_CC); } if (maxlen == 0 || maxchars == 0) { @@ -1787,8 +1787,6 @@ } if (php_stream_stat(src, &ssbuf) == 0) { - /* in the event that the source file is 0 bytes, return 1 to indicate success - * because opening the file to write had already created a copy */ if (ssbuf.sb.st_size == 0 #ifdef S_ISFIFO && !S_ISFIFO(ssbuf.sb.st_mode) @@ -1797,7 +1795,7 @@ && !S_ISCHR(ssbuf.sb.st_mode) #endif ) { - return 1; + return 0; } } @@ -1829,14 +1827,14 @@ while(towrite) { didwrite = php_stream_write_unicode(dest, writeptr, towrite); if (didwrite == 0) { - return 0; /* error */ + return PHP_STREAM_FAILURE; } towrite -= didwrite; writeptr += didwrite; } } else { - return haveread; + break; } if (maxchars == 0 || maxlen - haveread == 0) { @@ -1844,18 +1842,36 @@ } } - return haveread; + /* we've got at least 1 byte to read. + * less than 1 is an error */ + + if (haveread > 0) { + return haveread; + } + return PHP_STREAM_FAILURE; +} + +/* see _php_stream_copy_to_stream() */ +ZEND_ATTRIBUTE_DEPRECATED +PHPAPI size_t _php_stream_ucopy_to_stream(php_stream *src, php_stream *dest, size_t maxlen, size_t maxchars STREAMS_DC TSRMLS_DC) +{ + size_t ret = _php_stream_ucopy_to_stream_ex(src, dest, maxlen, maxchars STREAMS_REL_CC TSRMLS_CC); + if (ret == 0 && maxlen != 0 && maxchars != 0) { + return 1; + } + return ret; } /* Optimized for copying octets from source stream */ -PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC) +/* Returns the number of bytes moved, or PHP_STREAM_FAILURE on failure. */ +PHPAPI size_t _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC) { size_t haveread = 0; php_stream_statbuf ssbuf; if (src->readbuf_type == IS_UNICODE) { /* Called incorrectly, don't do that. */ - return _php_stream_ucopy_to_stream(src, dest, maxlen, -1 STREAMS_CC TSRMLS_CC); + return _php_stream_ucopy_to_stream_ex(src, dest, maxlen, -1 STREAMS_CC TSRMLS_CC); } if (maxlen == 0) { @@ -1867,8 +1883,6 @@ } if (php_stream_stat(src, &ssbuf) == 0) { - /* in the event that the source file is 0 bytes, return 1 to indicate success - * because opening the file to write had already created a copy */ if (ssbuf.sb.st_size == 0 #ifdef S_ISFIFO && !S_ISFIFO(ssbuf.sb.st_mode) @@ -1877,7 +1891,7 @@ && !S_ISCHR(ssbuf.sb.st_mode) #endif ) { - return 1; + return 0; } } @@ -1891,8 +1905,14 @@ mapped = php_stream_write(dest, p, mapped); php_stream_mmap_unmap(src); + + /* we've got at least 1 byte to read. + * less than 1 is an error */ - return mapped; + if (mapped > 0) { + return mapped; + } + return PHP_STREAM_FAILURE; } } @@ -1919,14 +1939,14 @@ while(towrite) { didwrite = php_stream_write(dest, writeptr, towrite); if (didwrite == 0) { - return 0; /* error */ + return PHP_STREAM_FAILURE; } towrite -= didwrite; writeptr += didwrite; } } else { - return haveread; + break; } if (maxlen - haveread == 0) { @@ -1934,7 +1954,26 @@ } } - return haveread; + /* we've got at least 1 byte to read. + * less than 1 is an error */ + + if (haveread > 0) { + return haveread; + } + return PHP_STREAM_FAILURE; +} + +/* Returns the number of bytes moved. + * Returns 1 when source len is 0. + * Deprecated in favor of php_stream_copy_to_stream_ex() */ +ZEND_ATTRIBUTE_DEPRECATED +PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC) +{ + size_t ret = _php_stream_copy_to_stream_ex(src, dest, maxlen STREAMS_REL_CC TSRMLS_CC); + if (ret == 0 && maxlen != 0) { + return 1; + } + return ret; } /* }}} */
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php