php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81396 phar:// file fflush breaks file write
Submitted: 2021-08-29 11:03 UTC Modified: 2021-08-30 12:43 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: dixyes at gmail dot com Assigned:
Status: Verified Package: PHAR related
PHP Version: 8.0.10 OS: irrelative
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: dixyes at gmail dot com
New email:
PHP Version: OS:

 

 [2021-08-29 11:03 UTC] dixyes at gmail dot com
Description:
------------
fflush file make strange result in phar files.

this occurs on PHP 7.4 also, tested in windows and linux

use php -n -dphar.readonly=0 test.php to test

Test script:
---------------
<?php

// make up the phar
$phar = new \Phar(__DIR__ . DIRECTORY_SEPARATOR . 'test.phar');
$phar->addEmptyDir('dir'); // may be irrelative
$phar->stopBuffering();

//$phar_uri = __DIR__ . DIRECTORY_SEPARATOR . 'file';
$phar_uri = 'phar://' . __DIR__ . DIRECTORY_SEPARATOR . 'test.phar' . DIRECTORY_SEPARATOR . 'dir/file';

// make a file
$f = fopen($phar_uri, 'w+');
fwrite($f, 'this is content ');
fflush($f); // comment out this, things will be ok
fwrite($f, "of file.\n");
fclose($f);

// read it
$f = fopen($phar_uri, 'r');
$content = fread($f, 4096);
if ($content !== "this is content of file.\n") {
    throw new Exception("bad file context: $content vs 'this is content of file.\n'");
} else {
    echo "OK\n";
}

Expected result:
----------------
OK

Actual result:
--------------
bad file context with strange things

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-30 12:43 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-08-30 12:43 UTC] cmb@php.net
When fflush() is called, the complete phar is written to disk, and
during this operation the connection to the $phar_uri file pointer
is severed[1].  Further writes to $phar_uri are then ignored by
the phar, except for the phar entry's size increment.  This leads
to the garbage bytes at the end of the string.

It might not be possible to retain the connection with the file
pointer.

[1] <https://github.com/php/php-src/blob/php-7.4.23/ext/phar/phar.c#L3069-L3070>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 13 23:01:28 2024 UTC