]> granicus.if.org Git - php/commitdiff
MFB5.3: Fixed bug #47997 (stream_copy_to_stream returns 1 on empty streams)
authorArnaud Le Blanc <lbarnaud@php.net>
Sun, 19 Apr 2009 14:44:09 +0000 (14:44 +0000)
committerArnaud Le Blanc <lbarnaud@php.net>
Sun, 19 Apr 2009 14:44:09 +0000 (14:44 +0000)
NEWS
ext/standard/file.c
ext/standard/streamsfuncs.c
ext/standard/tests/streams/bug47997.phpt [new file with mode: 0644]
main/php_streams.h
main/streams/cast.c
main/streams/streams.c

diff --git a/NEWS b/NEWS
index cd96b6d082319b7b0a1f829d349f8ce22cab1c4d..d89f8ab5a286e6b600ca8215ad1d8d9c7d0502d6 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,7 @@ PHP                                                                        NEWS
 - Fixed memory leak in ob_get_clean/ob_get_flush. (Christian)
 - Fixed segfault on invalid session.save_path. (Hannes)
 
+- Fixed bug #47997 (stream_copy_to_stream returns 1 on empty streams). (Arnaud)
 - Fixed bug #47981 (error handler not called regardless). (Hannes)
 - Fixed bug #47969 (ezmlm_hash() returns different values depend on OS).
   (Ilia)
index 748b2cdc880400d0e032679aee99ab733eebc932..9007d425d03ffb3976b35165747936b22a9ec8a9 100644 (file)
@@ -630,7 +630,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:
@@ -1836,7 +1839,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 cd751618ccf46443f2044b8da13fb5bbb559c2b1..60d6b441627662c55b56c193e3fac61622210b7b 100644 (file)
@@ -437,6 +437,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;
@@ -450,7 +451,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);
 }
 /* }}} */
 
diff --git a/ext/standard/tests/streams/bug47997.phpt b/ext/standard/tests/streams/bug47997.phpt
new file mode 100644 (file)
index 0000000..1ad2890
--- /dev/null
@@ -0,0 +1,13 @@
+--TEST--
+Bug #47997 (stream_copy_to_stream returns 1 on empty streams)
+--FILE--
+<?php
+
+$in = fopen('data://text/plain,', 'rb+');
+$out = fopen('php://memory', 'wb+');
+
+var_dump(stream_copy_to_stream($in, $out));
+
+?>
+--EXPECT--
+int(0)
index 4a0a446f42bfc8454f9cc8c33bbbba2b5e850057..4986287025cf5684f316eaf1d6b648fe88791976 100755 (executable)
@@ -416,9 +416,13 @@ 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()
 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 5a62c3a2d13abb91acdcb131625d27fe77145db1..82ee4636ed9c5e2fcea6e3d264fecf193ad300fe 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);
@@ -327,7 +327,7 @@ PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstr
        if (*newstream == NULL)
                return PHP_STREAM_FAILED;
 
-       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 64d0961cb5711913cb9a3f24da269cc826e4d50c..393655a68ad3b6651e9c4116f9488e71bebedf75 100755 (executable)
@@ -1288,7 +1288,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;
@@ -1305,8 +1306,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)
@@ -1315,7 +1314,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;
                }
        }
 
@@ -1329,8 +1328,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;
                }
        }
 
@@ -1354,22 +1359,40 @@ 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) {
                        break;
                }
        }
-       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() */
+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;
 }
 /* }}} */