Skip to content

Commit 1dd866d

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix GH-20302: Freeing a phar alias may invalidate PharFileInfo objects
2 parents 2e0ab27 + ae7117b commit 1dd866d

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

ext/phar/phar_object.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4514,6 +4514,9 @@ PHP_METHOD(PharFileInfo, __construct)
45144514
entry_obj->entry = entry_info;
45154515
if (!entry_info->is_persistent && !entry_info->is_temp_dir) {
45164516
++entry_info->fp_refcount;
4517+
/* The phar data must exist to keep the alias locked. */
4518+
ZEND_ASSERT(!phar_data->is_persistent);
4519+
++phar_data->refcount;
45174520
}
45184521

45194522
ZVAL_STRINGL(&arg1, fname, fname_len);
@@ -4544,23 +4547,26 @@ PHP_METHOD(PharFileInfo, __destruct)
45444547
RETURN_THROWS();
45454548
}
45464549

4547-
if (!entry_obj->entry) {
4550+
phar_entry_info *entry = entry_obj->entry;
4551+
if (!entry) {
45484552
return;
45494553
}
45504554

4551-
if (entry_obj->entry->is_temp_dir) {
4552-
if (entry_obj->entry->filename) {
4553-
zend_string_efree(entry_obj->entry->filename);
4554-
entry_obj->entry->filename = NULL;
4555+
if (entry->is_temp_dir) {
4556+
if (entry->filename) {
4557+
zend_string_release_ex(entry->filename, false);
4558+
entry->filename = NULL;
45554559
}
45564560

4557-
efree(entry_obj->entry);
4558-
} else if (!entry_obj->entry->is_persistent) {
4559-
--entry_obj->entry->fp_refcount;
4560-
/* It is necessarily still in the manifest, which will ultimately free this. */
4561+
efree(entry);
4562+
entry_obj->entry = NULL;
4563+
} else if (!entry->is_persistent) {
4564+
--entry->fp_refcount;
4565+
/* The entry itself still lives in the manifest,
4566+
* which will either be freed here if the file info was the last reference; or freed later. */
4567+
entry_obj->entry = NULL;
4568+
phar_archive_delref(entry->phar);
45614569
}
4562-
4563-
entry_obj->entry = NULL;
45644570
}
45654571
/* }}} */
45664572

ext/phar/tests/gh20302.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
GH-20302 (Freeing a phar alias may invalidate PharFileInfo objects)
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.require_hash=0
7+
--FILE--
8+
<?php
9+
$fname = __DIR__.'/gh20302.phar';
10+
$pname = 'phar://' . $fname;
11+
$file = "<?php
12+
__HALT_COMPILER(); ?>";
13+
$files = array();
14+
$files['here'] = 'a';
15+
include __DIR__.'/files/phar_test.inc';
16+
$b = new PharFileInfo($pname . '/here');
17+
18+
// Create new phar with same alias and open it
19+
@mkdir(__DIR__.'/gh20302');
20+
$fname = __DIR__.'/gh20302/gh20302.phar';
21+
$pname = 'phar://' . $fname;
22+
include __DIR__.'/files/phar_test.inc';
23+
try {
24+
new Phar($fname);
25+
} catch (UnexpectedValueException $e) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
?>
29+
--CLEAN--
30+
<?php
31+
@unlink(__DIR__.'/gh20302/gh20302.phar');
32+
@unlink(__DIR__.'/gh20302.phar');
33+
@rmdir(__DIR__.'/gh20302');
34+
?>
35+
--EXPECTF--
36+
Cannot open archive "%sgh20302.phar", alias is already in use by existing archive

0 commit comments

Comments
 (0)