]> granicus.if.org Git - php/commitdiff
Fixed bug #47997 (stream_copy_to_stream returns 1 on empty streams)
authorArnaud Le Blanc <lbarnaud@php.net>
Sun, 19 Apr 2009 13:46:47 +0000 (13:46 +0000)
committerArnaud Le Blanc <lbarnaud@php.net>
Sun, 19 Apr 2009 13:46:47 +0000 (13:46 +0000)
NEWS
ext/standard/file.c
ext/standard/streamsfuncs.c
main/php_streams.h
main/streams/cast.c
main/streams/streams.c

diff --git a/NEWS b/NEWS
index c1c41e6014e957041b72005662d1b9b8dcff5d9d..996485ddf225b267e129e89aa3f6ee545b15cd10 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,7 @@ PHP                                                                        NEWS
   disable this behaviour using "http"=>array("auto_decode"=>0) in stream
   context. (Dmitry)
 
+- Fixed bug #47997 (stream_copy_to_stream returns 1 on empty streams). (Arnaud)
 - Fixed bug #47880 (crashes in call_user_func_array()). (Dmitry)
 - Fixed bug #47856 (stristr() converts needle to lower-case). (Ilia)
 - Fixed bug #47851 (is_callable throws fatal error). (Dmitry)
index 192d7f1cc27b6318913d4feae9682760fcc73cc0..a3b96b3b1b59e5cef1eef61cb58ba831ae7c5466 100644 (file)
@@ -627,7 +627,10 @@ PHP_FUNCTION(file_put_contents)
 
        switch (Z_TYPE_P(data)) {
                case IS_RESOURCE:
-                       numbytes = php_stream_copy_to_stream(srcstream, stream, PHP_STREAM_COPY_ALL);
+                       numbytes = (int) php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL);
+                       if ((size_t)numbytes == PHP_STREAM_FAILURE) {
+                               numbytes = -1;
+                       }
                        break;
                case IS_NULL:
                case IS_LONG:
@@ -1783,7 +1786,7 @@ safe_to_copy:
        deststream = php_stream_open_wrapper(dest, "wb", ENFORCE_SAFE_MODE | 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);
index cdb420c0f73510fd2244d4840f273d413a46635a..426849e81e85c7ea81364922b82681a1e8a8e618 100644 (file)
@@ -443,6 +443,7 @@ PHP_FUNCTION(stream_copy_to_stream)
        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;
@@ -456,7 +457,12 @@ PHP_FUNCTION(stream_copy_to_stream)
                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);
 }
 /* }}} */
 
index 5c699f2e970964c49180580a16630c6d5f039fe4..fab62c19af47b00755a14280621b4fe33ad44a15 100755 (executable)
@@ -420,9 +420,14 @@ END_EXTERN_C()
  * 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_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC);
 #define php_stream_copy_to_stream(src, dest, maxlen)   _php_stream_copy_to_stream((src), (dest), (maxlen) STREAMS_CC TSRMLS_CC)
+PHPAPI size_t _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC);
+#define php_stream_copy_to_stream_ex(src, dest, maxlen)        _php_stream_copy_to_stream_ex((src), (dest), (maxlen) STREAMS_CC TSRMLS_CC)
 
 
 /* read all data from stream and put into a buffer. Caller must free buffer when done.
index dea72da53d18cb0ade85507ccdf66c060f6350b4..06805c709c9faad4ee077ba4f451224234dcc397 100644 (file)
@@ -214,9 +214,9 @@ PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show
 
                        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 @@ PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstr
        (*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;
index 6d9d2ecd0e95cf42e1f826662495fdf1020eae23..c5aebe7812730181c4d3108da7eea258d4966e0a 100755 (executable)
@@ -1297,7 +1297,8 @@ PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen
        return len;
 }
 
-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)
 {
        char buf[CHUNK_SIZE];
        size_t readchunk;
@@ -1314,8 +1315,6 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size
        }
 
        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)
@@ -1324,7 +1323,7 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size
                 && !S_ISCHR(ssbuf.sb.st_mode)
 #endif
                ) {
-                       return 1;
+                       return 0;
                }
        }
 
@@ -1338,8 +1337,14 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size
                        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;
                }
        }
 
@@ -1364,14 +1369,14 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size
                        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) {
@@ -1379,7 +1384,26 @@ PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size
                }
        }
 
-       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;
 }
 /* }}} */