php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52335 fseek() on memory stream behavior different then file
Submitted: 2010-07-14 13:18 UTC Modified: 2017-10-24 07:52 UTC
Votes:5
Avg. Score:3.4 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: jbondc at openmv dot com Assigned:
Status: Closed Package: Streams related
PHP Version: 5.2.14RC2 OS: FreeBSD 7.2
Private report: No CVE-ID: None
 [2010-07-14 13:18 UTC] jbondc at openmv dot com
Description:
------------
May not be a bug but a documentation issue

With a file, fseek() to an offset that doesn't exist results in NULL bytes added.
Using a memory stream, fseek() just fails.

Which one is the expected behavior? Also for streamWrapper::stream_seek()








Test script:
---------------
$sp = fopen("php://memory", 'w+');
fseek($sp, 1024);               // fails why?
echo fwrite($sp, "abc");        // 3
fseek($sp, 1024);               //
echo fread($sp, 3);             // NULL.. 

$sp = fopen("/tmp/foo", 'w+');
fseek($sp, 1024);
echo fwrite($sp, "abc");  	// 3
fseek($sp, 1024);
echo fread($sp, 3);	        // abc

Expected result:
----------------
3abc3abc

Actual result:
--------------
33abc

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-12 17:53 UTC] cataphract@php.net
-Type: Bug +Type: Documentation Problem -Assigned To: +Assigned To: cataphract
 [2010-10-22 15:43 UTC] kalle@php.net
-Package: Streams related +Package: Documentation problem
 [2010-11-13 16:09 UTC] cataphract@php.net
Automatic comment from SVN on behalf of cataphract
Revision: http://svn.php.net/viewvc/?view=revision&revision=305317
Log: - Behavior when seeking past the end of file.
  Partially addresses bug #52335.
- Expanded note on how the stream may not support seeking.
 [2010-11-13 16:10 UTC] cataphract@php.net
-Type: Documentation Problem +Type: Bug -Package: Documentation problem +Package: Streams related
 [2010-11-13 16:10 UTC] cataphract@php.net
I've fixed the doc part, but on a closer inspection the behavior of the memory stream on past-the-end-of-file seeks could be improved.
 [2010-11-22 14:55 UTC] jbondc at openmv dot com
Thanks, saw the changes in fseek.xml

What about adding streamWrapper::hasFeature(/* int */$featureId)

const STREAM_FEAT_FWDSEEK = 1;
const STREAM_FEAT_OTHER   = 2; 
//...

$stream = new File_Stream('/tmp/foo');
$stream->isSupported(STREAM_FEAT_FWDSEEK); // true
fseek($stream, 1024); // NULL bytes added

$stream = new Memory_Stream('/tmp/foo');
$stream->isSupported(STREAM_FEAT_FWDSEEK); // false
fseek($stream, 1024); // false

The goal is to have a consistent api to write to a binary stream.

I can give it some more thought, other stream 'features'.
 [2017-10-24 07:52 UTC] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: cataphract +Assigned To:
 [2023-03-13 04:54 UTC] belisoful at icloud dot com
I am making a TDataWriter class that acts as a wrapper for writing data to a seekable stream.  I was trying to figure out why it fseek was failing when moving after the end but then found this bug.  I was using memory.  It happens on temp as well even though temp is sometimes a file and sometimes not a file (aka cache below a threshold)

Writing to memory should add the null bytes as happens with files.   I have a stream wrapper, so i can easily check the stream and if fseeking is past the end, then to ftruncate the stream larger, but getting to this point is just adding difficulty where there should be none.

In some instances my class would be writing to memory and others it might be writing to a file.  Standardizing this behavior is important.

It should be documented until it is corrected.
 [2023-08-28 12:32 UTC] git@php.net
Automatic comment on behalf of bukka
Revision: https://github.com/php/php-src/commit/ba9650d697b9433061c0396d08d111f433cae43a
Log: Fix bug #52335 (fseek() on memory stream behavior different then file)
 [2023-08-28 12:32 UTC] git@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Mar 03 09:01:28 2024 UTC