]> granicus.if.org Git - php/commitdiff
Fix bug #68532: convert.base64-encode omits padding bytes
authormcq8 <php@mcq8.be>
Thu, 4 Dec 2014 16:36:44 +0000 (17:36 +0100)
committerJulien Pauli <jpauli@php.net>
Fri, 12 Dec 2014 15:42:38 +0000 (16:42 +0100)
ext/standard/tests/file/bug68532.phpt [new file with mode: 0644]
ext/standard/tests/file/stream_rfc2397_007.phpt
main/streams/memory.c

diff --git a/ext/standard/tests/file/bug68532.phpt b/ext/standard/tests/file/bug68532.phpt
new file mode 100644 (file)
index 0000000..7d1a0ce
--- /dev/null
@@ -0,0 +1,25 @@
+--TEST--
+Bug #68532: convert.base64-encode omits padding bytes
+--FILE--
+<?php
+$testString = 'test';
+$stream = fopen('php://memory','r+');
+fwrite($stream, $testString);
+rewind($stream);
+$filter = stream_filter_append($stream, 'convert.base64-encode');
+echo "memoryStream = " . stream_get_contents($stream).PHP_EOL;
+
+
+$fileStream = fopen(__DIR__ . '/base64test.txt','w+');
+fwrite($fileStream , $testString);
+rewind($fileStream );
+$filter = stream_filter_append($fileStream , 'convert.base64-encode');
+echo "fileStream = " . stream_get_contents($fileStream ).PHP_EOL;
+?>
+--CLEAN--
+<?php
+unlink(__DIR__ . '/base64test.txt');
+?>
+--EXPECT--
+memoryStream = dGVzdA==
+fileStream = dGVzdA==
index 8a6f3155dd79d6c3809a2916e00fb7592e11de96..b62f19cd37427a5357edb153c750aadebb317b83 100644 (file)
@@ -27,6 +27,7 @@ foreach($streams as $stream)
        var_dump(feof($fp));
        echo "===GETC===\n";
        var_dump(fgetc($fp));
+       var_dump(fgetc($fp));
        var_dump(ftell($fp));
        var_dump(feof($fp));
        echo "===REWIND===\n";
@@ -94,6 +95,7 @@ int(5)
 bool(false)
 ===GETC===
 string(1) "5"
+bool(false)
 int(6)
 bool(true)
 ===REWIND===
index e6d0baaa86d2ce7eda90404d3c37861d99662822..f73c12f7d7137ef68c1d54bc9fe7ae4c9e7bb194 100644 (file)
@@ -87,15 +87,19 @@ static size_t php_stream_memory_read(php_stream *stream, char *buf, size_t count
        php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
        assert(ms != NULL);
 
-       if (ms->fpos + count >= ms->fsize) {
-               count = ms->fsize - ms->fpos;
+       if (ms->fpos == ms->fsize) {
                stream->eof = 1;
-       }
-       if (count) {
-               assert(ms->data!= NULL);
-               assert(buf!= NULL);
-               memcpy(buf, ms->data+ms->fpos, count);
-               ms->fpos += count;
+               count = 0;
+       } else {
+               if (ms->fpos + count >= ms->fsize) {
+                       count = ms->fsize - ms->fpos;
+               }
+               if (count) {
+                       assert(ms->data!= NULL);
+                       assert(buf!= NULL);
+                       memcpy(buf, ms->data+ms->fpos, count);
+                       ms->fpos += count;
+               }
        }
        return count;
 }