Skip to content

phar trying to allocate insane memory sizes  #8828

@rainerjung

Description

@rainerjung

Description

Lots of phar unit tests fail, eg. ext/phar/tests/018.phar.phpt.

The error message is

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 18446604268229099552 bytes) in /path/to/ext/phar/tests/018.php on line 13

Note that the demanded memory size "18446604268229099552" does not make sense.

I extracted a standalone test case from 018. This test case runs for PHP 8.1, but not for 8.2.0alpha1. It uses two steps:

  • x.php creates x.phar.php. It is based on 018.phpt plus a simplified inlined ext/phar/tests/files/phar_test.inc. Running it produces binary identical files for PHP 8.1 and 8.2.

  • Running "/path/to/bin/php -d extension=phar -d phar.require_hash=Off x.phar.php" works for 8.1 (no error, no output), but fails with the above allocation error.

Contents of x.php:

<?php
$fname = 'x.phar.php';
$file = "<?php
__HALT_COMPILER(); ?>";
$files = array();
$files['b/a'] = 'b';

date_default_timezone_set('UTC');

$manifest = (binary)'';
$gflags = 0;

foreach($files as $name => $cont)
{
    global $gflags, $files;

    $comp = $cont;
    $ulen = strlen($cont);
    $clen = strlen($comp);
    $crc32= crc32((binary)$cont);
    $time = @mktime(12, 0, 0, 3, 1, 2006);
    $flags= 0;
    $perm = 0x000001B6;
    $meta = "";

    // write manifest entry
    $manifest .= pack('V', strlen($name)) . (binary)$name;
    $manifest .= pack('VVVVVV', $ulen, $time, $clen, $crc32, $flags|$perm, strlen($meta)) . (binary)$meta;

    // globals
    $gflags |= $flags;
    $files[$name] = $comp;
}

$alias = 'hio';

$pmeta = '';

$manifest = pack('VnVV', count($files), isset($hasdir) ? 0x1110 : 0x1000, $gflags, strlen($alias)) . (binary)$alias . pack('V', strlen($pmeta)) . (binary)$pmeta . $manifest;

$file = (binary)$file;
$file .= pack('V', strlen($manifest)) . $manifest;

foreach($files as $cont)
{
    $file .= (binary)$cont;
}

file_put_contents($fname, $file);
?>

Resulted in this output:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 18446603465070215200 bytes) in Unknown on line 0

The problem seems to be related to phar file contents within directories. Using "$files['b/a'] = 'b';" shows the problem, using "$files['bba'] = 'b';" works.

PHP Version

PHP 8.2.0alpha1

Operating System

Linux RHEL 8

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions