php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66638 Using SEEK_CUR to seek before start in php://memory produces incorrect results
Submitted: 2014-02-03 23:58 UTC Modified: 2014-02-04 12:34 UTC
From: madhusudancs at gmail dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: master-Git-2014-02-03 (Git) OS: Linux (likely all)
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
49 - 25 = ?
Subscribe to this entry?

 
 [2014-02-03 23:58 UTC] madhusudancs at gmail dot com
Description:
------------
When using php://memory stream, attempting to seek behind the start position, produces incorrect results.

Sample test case below (in the next field)

Test case should print the entire stream (all the bytes in the stream, but it prints an empty string.



Test script:
---------------
<?php
$fh = fopen('php://memory', 'w');

for ($ii = 0; $ii < 10; $ii++) {
   fwrite($fh, "[$ii] hello world. ");
}

$sz = 170;

fseek($fh, $sz - 100, SEEK_SET);

fseek($fh, -$sz, SEEK_CUR);

var_dump(fread($fh, $sz));
?>


Expected result:
----------------
string(170) "[0] hello world. [1] hello world. [2] hello world. [3] hello world. [4] hello world. [5] hello world. [6] hello world. [7] hello world. [8] hello world. [9] hello world. "

Actual result:
--------------
string(0) ""

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-02-04 02:00 UTC] madhusudancs at gmail dot com
-Summary: Using SEEK_CUR to seek behind start in a produces incorrect result +Summary: Using SEEK_CUR to seek before start in php://memory produces incorrect results
 [2014-02-04 02:00 UTC] madhusudancs at gmail dot com
Updating the summary line
 [2014-02-04 12:34 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2014-02-04 12:34 UTC] krakjoe@php.net
The second call to fseek returns -1, which you should be checking for, you get unexpected results as a result of not checking return values. 

I'm not sure why you expect to be able to read before the start of a memory stream, but this isn't a bug, the code is wrong.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 10 22:01:26 2025 UTC