php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77081 ftruncate() changes seek pointer in c mode
Submitted: 2018-10-29 15:21 UTC Modified: 2018-10-30 22:53 UTC
From: cmb@php.net Assigned: ab (profile)
Status: Closed Package: Filesystem function related
PHP Version: 7.3Git-2018-10-29 (Git) OS: Windows
Private report: No CVE-ID: None
 [2018-10-29 15:21 UTC] cmb@php.net
Description:
------------
The documentation of fopen() mode c[1] states:

| The file pointer is positioned on the beginning of the file.

However, this does not happen with PHP 7.3 on Windows; instead the
file pointer is positioned at the end of the file, even though
ftell() claims otherwise.  It works as expected on Linux and with
PHP 7.2 on Windows.

[1] <http://php.net/manual/en/function.fopen.php>


Test script:
---------------
<?php
$filename = __DIR__ . '/fopen-c.txt';
file_put_contents($filename, 'foo');
$stream = fopen($filename, 'c');
ftruncate($stream, 0);
// fseek($stream, 0);
var_dump(ftell($stream));
fwrite($stream, 'bar');
fclose($stream);
var_dump(file_get_contents($filename));


Expected result:
----------------
int(0)
string(3) "bar"


Actual result:
--------------
int(0)
string(6) "   bar"

Patches

restore-old-position (last revision 2018-10-29 22:31 UTC by cmb@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-29 22:31 UTC] cmb@php.net
The following patch has been added/updated:

Patch Name: restore-old-position
Revision:   1540852290
URL:        https://bugs.php.net/patch-display.php?bug=77081&patch=restore-old-position&revision=1540852290
 [2018-10-29 22:31 UTC] cmb@php.net
-Assigned To: +Assigned To: ab
 [2018-10-29 22:31 UTC] cmb@php.net
This regression has been introduced with commit 7728160[1].
Anatol, what do you think about something like the attached
restore-old-position patch?

[1] <http://git.php.net/?p=php-src.git;a=commit;h=7728160784785771d1933f5f0a8c4d7a735470b0>
 [2018-10-30 18:05 UTC] ab@php.net
Thanks for the ping, Christoph. Perhaps, if there's something wrong with fopen, it should be fixed in first place. I'm going to check it first.

Thanks.
 [2018-10-30 18:16 UTC] cmb@php.net
-Summary: fopen() c mode positions file pointer at the end +Summary: ftruncate() changes seek pointer in c mode
 [2018-10-30 18:16 UTC] cmb@php.net
> […] if there's something wrong with fopen […]

Sorry, my mistake.  Actually, the problem is not with fopen() per
se, but rather with ftruncate() on a file freshly opened in c
mode.  If I comment out the ftruncate() call in the given reproduce
script, I get the expected:

    int(0)
    string(3) "bar"

Also, writing only the single character 'b' instead of 'bar'
gives the expected:

    int(0)
    string(3) "boo"

The problem is that the ftruncate() call changes the seek pointer,
which it is not supposed to.
 [2018-10-30 19:18 UTC] ab@php.net
I see. As fopen() with 'c' doesn't truncate, the file pointer suddenly jumps to the end of file after ftruncate(). This is obviously wrong leftover from the linked patch for bug #76803. I'm going to fix it using your solution and revamp some more.

Thanks.
 [2018-10-30 19:51 UTC] ab@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=8827cc34cf8c77828330182ee1e6d0b9438d489c
Log: Fixed bug #77081 ftruncate() changes seek pointer in c mode
 [2018-10-30 19:51 UTC] ab@php.net
-Status: Assigned +Status: Closed
 [2018-10-30 22:53 UTC] cmb@php.net
Thanks!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC