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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: madhusudancs at gmail dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 21:01:27 2024 UTC