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
 [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

Pull Requests

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>
 [2024-07-24 06:24 UTC] jeffrey597doss at outlook dot com
I have read your article, it is very informative and helpful for me. I admire the valuable information you offer in your articles. Thanks for posting it.. (https://github.com)(https://www.marykayin-touch.com)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC