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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
10 + 29 = ?
Subscribe to this entry?

 
 [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: Fri Apr 26 14:01:29 2024 UTC