Skip to content

Commit 98bb934

Browse files
committed
Fix memory leak on failure in phar_convert_to_other()
Closes GH-19755.
1 parent c50b37d commit 98bb934

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ PHP NEWS
4949
. Fixed memory leaks when verifying OpenSSL signature. (Girgias)
5050
. Fix memory leak in phar tar temporary file error handling code. (nielsdos)
5151
. Fix metadata leak when phar convert logic fails. (nielsdos)
52+
. Fix memory leak on failure in phar_convert_to_other(). (nielsdos)
5253

5354
- Standard:
5455
. Fixed bug GH-16649 (UAF during array_splice). (alexandre-daubois)

ext/phar/phar_object.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,12 @@ static zend_object *phar_convert_to_other(phar_archive_data *source, int convert
22292229
PHAR_G(last_phar) = NULL;
22302230
PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
22312231

2232+
php_stream *tmp_fp = php_stream_fopen_tmpfile();
2233+
if (tmp_fp == NULL) {
2234+
zend_throw_exception_ex(phar_ce_PharException, 0, "unable to create temporary file");
2235+
return NULL;
2236+
}
2237+
22322238
phar = (phar_archive_data *) ecalloc(1, sizeof(phar_archive_data));
22332239
/* set whole-archive compression and type from parameter */
22342240
phar->flags = flags;
@@ -2253,11 +2259,7 @@ static zend_object *phar_convert_to_other(phar_archive_data *source, int convert
22532259
zend_hash_init(&phar->virtual_dirs, sizeof(char *),
22542260
zend_get_hash_value, NULL, 0);
22552261

2256-
phar->fp = php_stream_fopen_tmpfile();
2257-
if (phar->fp == NULL) {
2258-
zend_throw_exception_ex(phar_ce_PharException, 0, "unable to create temporary file");
2259-
return NULL;
2260-
}
2262+
phar->fp = tmp_fp;
22612263
phar->fname = source->fname;
22622264
phar->fname_len = source->fname_len;
22632265
phar->is_temporary_alias = source->is_temporary_alias;

0 commit comments

Comments
 (0)