php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34743 fread doesn't move internal "position" pointer. fseek fails as a result
Submitted: 2005-10-05 18:21 UTC Modified: 2005-10-05 19:18 UTC
From: alexander dot v at zend dot com Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 6CVS-2005-10-05 (CVS) OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alexander dot v at zend dot com
New email:
PHP Version: OS:

 

 [2005-10-05 18:21 UTC] alexander dot v at zend dot com
Description:
------------
fread() doesn't move internal file pointer "stream->position" (could be retrived by ftell()).

fseek(...,...,SEEK_CUR) fails if crosses buffer boundary as a result.

Reproduce code:
---------------
// 'testdata.dat' is a binary file.
// Each byte contains value equal to offset % 256
$f = fopen('testdata.dat', 'r');

printf( "offset (before read): 0x%X\n", ftell($f) );
$bin_str = fread($f, 16);
for ($count = 0; $count < 16; $count++) {
    printf( "0x%02X ", ord($bin_str{$count}) );
}
printf( "\noffset (after read): 0x%X\n.....\n", ftell($f) );

fseek( $f, 16*1024, SEEK_CUR);
printf( "offset (before read): 0x%X\n", ftell($f) );
$bin_str = fread($f, 16);
for ($count = 0; $count < 16; $count++) {
    printf( "0x%02X ", ord($bin_str{$count}) );
}
printf( "\noffset (after read): 0x%X\n", ftell($f) );
fclose( $f );


Expected result:
----------------
offset (before read): 0x0
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F
offset (after read): 0x10
.....
offset (before read): 0x4010
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F
offset (after read): 0x4020


Actual result:
--------------
offset (before read): 0x0
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F
offset (after read): 0x0
.....
offset (before read): 0x4000
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F
offset (after read): 0x4000


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-05 18:27 UTC] alexander dot v at zend dot com
Patch:
----------------------------------------------
Index: main/streams/streams.c
===================================================================
RCS file: /repository/php-src/main/streams/streams.c,v
retrieving revision 1.87
diff -u -r1.87 streams.c
--- main/streams/streams.c	22 Aug 2005 14:48:25 -0000	1.87
+++ main/streams/streams.c	5 Oct 2005 16:06:53 -0000
@@ -855,6 +855,8 @@
 	*pnum_chars = num_chars;
 	*pis_unicode = is_unicode;
 
+	stream->position += num_bytes;
+
 	if (num_chars == 0 && grow_mode) {
 		efree(buf);
 		buf = NULL;
 [2005-10-05 19:18 UTC] tony2001@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 10:01:33 2025 UTC