cataphract                               Thu, 14 Oct 2010 03:15:15 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=304384

Log:
- [DOC] Reverted rev #304382 and rev #304380, as I figured out a way to
  fix the erratic behavior without breaking backwards compatibility. Namely,
  $offset retains SEEK_SET behavior but actually SEEK_CUR is passed to
  _php_stream_seek, if possible, by moving the offset stream->position bytes.
- Addresses bug #53006.

Bugs: http://bugs.php.net/304382 (error getting bug information)
      http://bugs.php.net/304380 (error getting bug information)
      http://bugs.php.net/53006 (Assigned) stream_get_contents offset max is 
1165
      
Changed paths:
    U   php/php-src/branches/PHP_5_3/ext/standard/streamsfuncs.c
    U   php/php-src/trunk/ext/standard/streamsfuncs.c
    U   php/php-src/trunk/ext/standard/tests/streams/bug46426.phpt
    U   
php/php-src/trunk/ext/standard/tests/streams/stream_get_contents_001.phpt

Modified: php/php-src/branches/PHP_5_3/ext/standard/streamsfuncs.c
===================================================================
--- php/php-src/branches/PHP_5_3/ext/standard/streamsfuncs.c    2010-10-14 
02:47:31 UTC (rev 304383)
+++ php/php-src/branches/PHP_5_3/ext/standard/streamsfuncs.c    2010-10-14 
03:15:15 UTC (rev 304384)
@@ -415,7 +415,7 @@
 {
        php_stream *stream;
        zval *zsrc;
-       long maxlen = PHP_STREAM_COPY_ALL, pos = 0;
+       long maxlen = PHP_STREAM_COPY_ALL, pos = -1L;
        int len, newlen;
        char *contents = NULL;

@@ -425,9 +425,19 @@

        php_stream_from_zval(stream, &zsrc);

-       if ((pos > 0 || (pos == 0 && ZEND_NUM_ARGS() > 2)) && 
php_stream_seek(stream, pos, SEEK_SET) < 0) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek to 
position %ld in the stream", pos);
-               RETURN_FALSE;
+       if (pos >= 0) {
+               int seek_res = 0;
+               if (pos > stream->position) {
+                       /* use SEEK_CUR to allow emulation in streams that 
don't support seeking */
+                       seek_res = php_stream_seek(stream, pos - 
stream->position, SEEK_CUR);
+               } else if (pos < stream->position)  {
+                       seek_res = php_stream_seek(stream, pos, SEEK_SET);
+               }
+
+               if (seek_res != 0) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to 
seek to position %ld in the stream", pos);
+                       RETURN_FALSE;
+               }
        }

        len = php_stream_copy_to_mem(stream, &contents, maxlen, 0);

Modified: php/php-src/trunk/ext/standard/streamsfuncs.c
===================================================================
--- php/php-src/trunk/ext/standard/streamsfuncs.c       2010-10-14 02:47:31 UTC 
(rev 304383)
+++ php/php-src/trunk/ext/standard/streamsfuncs.c       2010-10-14 03:15:15 UTC 
(rev 304384)
@@ -415,7 +415,7 @@
 {
        php_stream *stream;
        zval *zsrc;
-       long maxlen = PHP_STREAM_COPY_ALL, pos = 0;
+       long maxlen = PHP_STREAM_COPY_ALL, pos = -1L;
        int len, newlen;
        char *contents = NULL;

@@ -425,12 +425,19 @@

        php_stream_from_zval(stream, &zsrc);

-       if ((pos > 0L) && php_stream_seek(stream, pos, SEEK_CUR) < 0) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek %ld 
bytes from current position in the stream", pos);
-               RETURN_FALSE;
-       } else if (pos < 0L) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of bytes to 
seek must be non-negative, given %ld", pos);
-               RETURN_FALSE;
+       if (pos >= 0) {
+               int seek_res = 0;
+               if (pos > stream->position) {
+                       /* use SEEK_CUR to allow emulation in streams that 
don't support seeking */
+                       seek_res = php_stream_seek(stream, pos - 
stream->position, SEEK_CUR);
+               } else if (pos < stream->position)  {
+                       seek_res = php_stream_seek(stream, pos, SEEK_SET);
+               }
+
+               if (seek_res != 0) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to 
seek to position %ld in the stream", pos);
+                       RETURN_FALSE;
+               }
        }

        len = php_stream_copy_to_mem(stream, &contents, maxlen, 0);

Modified: php/php-src/trunk/ext/standard/tests/streams/bug46426.phpt
===================================================================
--- php/php-src/trunk/ext/standard/tests/streams/bug46426.phpt  2010-10-14 
02:47:31 UTC (rev 304383)
+++ php/php-src/trunk/ext/standard/tests/streams/bug46426.phpt  2010-10-14 
03:15:15 UTC (rev 304384)
@@ -7,23 +7,19 @@

 fwrite($tmp, b"12345");

-fseek($tmp, 0);
-echo stream_get_contents($tmp, 2, 1); //23
+echo stream_get_contents($tmp, 2, 1);
 echo "\n";
-echo stream_get_contents($tmp, -1); //45
+echo stream_get_contents($tmp, -1);
 echo "\n";
-fseek($tmp, -1, SEEK_CUR);
-echo stream_get_contents($tmp, -1, 0); //5
+echo stream_get_contents($tmp, -1, 0);
 echo "\n";
-fseek($tmp, 0);
-echo stream_get_contents($tmp, -1, 2); //345
+echo stream_get_contents($tmp, -1, 2);
 echo "\n";
-fseek($tmp, 0);
-echo stream_get_contents($tmp, 0, 0); //""
+echo stream_get_contents($tmp, 0, 0);
 echo "\n";
-echo stream_get_contents($tmp, 1, 0); //1
+echo stream_get_contents($tmp, 1, 0);
 echo "\n";
-echo stream_get_contents($tmp, -1); //2345
+echo stream_get_contents($tmp, -1);

 @unlink($tmp);

@@ -31,7 +27,7 @@
 --EXPECT--
 23
 45
-5
+12345
 345

 1

Modified: 
php/php-src/trunk/ext/standard/tests/streams/stream_get_contents_001.phpt
===================================================================
--- php/php-src/trunk/ext/standard/tests/streams/stream_get_contents_001.phpt   
2010-10-14 02:47:31 UTC (rev 304383)
+++ php/php-src/trunk/ext/standard/tests/streams/stream_get_contents_001.phpt   
2010-10-14 03:15:15 UTC (rev 304384)
@@ -9,17 +9,14 @@

 echo stream_get_contents($tmp, 2, 5), "--\n";
 echo stream_get_contents($tmp, 2), "--\n";
-fseek($tmp, 0);
 echo stream_get_contents($tmp, 2, 3), "--\n";
 echo stream_get_contents($tmp, 2, -1), "--\n";

 @unlink($tmp);

 ?>
---EXPECTF--
+--EXPECT--
 --
 --
 45--
-
-Warning: stream_get_contents(): Number of bytes to seek must be non-negative, 
given -1 in %s on line %d
 --

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to