Skip to content

Commit c3d17d9

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix memory leak and invalid continuation after tar header writing fails
2 parents 58b45b5 + 7c85926 commit c3d17d9

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ PHP NEWS
3939
(Arnaud)
4040
. Fixed bug GH-19831 (function JIT may not deref property value). (Arnaud)
4141

42+
- Phar:
43+
. Fix memory leak and invalid continuation after tar header writing fails.
44+
(nielsdos)
45+
4246
- SimpleXML:
4347
. Fixed bug GH-19988 (zend_string_init with NULL pointer in simplexml (UB)).
4448
(nielsdos)

ext/phar/tar.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,16 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def
11701170
}
11711171

11721172
zend_hash_apply_with_argument(&phar->manifest, phar_tar_writeheaders, (void *) &pass);
1173-
/* TODO: memory leak and incorrect continuation if phar_tar_writeheaders fails? */
1173+
1174+
if (error && *error) {
1175+
if (closeoldfile) {
1176+
php_stream_close(oldfile);
1177+
}
1178+
1179+
/* on error in the hash iterator above, error is set */
1180+
php_stream_close(newfile);
1181+
return EOF;
1182+
}
11741183

11751184
/* add signature for executable tars or tars explicitly set with setSignatureAlgorithm */
11761185
if (!phar->is_data || phar->sig_flags) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
Tar flush with too long file name
3+
--EXTENSIONS--
4+
phar
5+
--SKIPIF--
6+
<?php
7+
if (getenv('SKIP_SLOW_TESTS')) die('skip');
8+
if (function_exists('openssl_sign')) die('skip requires openssl disabled for mocking purposes');
9+
?>
10+
--INI--
11+
phar.require_hash=0
12+
--FILE--
13+
<?php
14+
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.tar';
15+
16+
// Mock sign to fail at second invocation, tricks failure in phar_create_signature()
17+
function openssl_sign() {
18+
static $counter = 0;
19+
$counter++;
20+
if ($counter === 2) {
21+
return false;
22+
}
23+
return true;
24+
}
25+
26+
$phar = new PharData($fname);
27+
$phar->addEmptyDir('blah1/');
28+
$phar->setSignatureAlgorithm(Phar::OPENSSL, "randomcrap");
29+
try {
30+
$phar->addEmptyDir('blah2/' . str_repeat('X', 1000));
31+
} catch (PharException $e) {
32+
echo $e->getMessage();
33+
}
34+
35+
?>
36+
--CLEAN--
37+
<?php
38+
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.tar');
39+
?>
40+
--EXPECTF--
41+
tar-based phar "%s" cannot be created, filename "%s" is too long for tar file format

0 commit comments

Comments
 (0)