Skip to content

Commit ce0df1a

Browse files
committed
phar: Fix memory leak when opening temp file fails while trying to open gzip-compressed archive
`filterparams` can leak if `php_stream_fopen_tmpfile()` fails. To solve this, move the temp file creation first. Closes phpGH-20220.
1 parent cc83761 commit ce0df1a

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ PHP NEWS
5757
of type size_t. (Girgias)
5858
. Fix memory leak when openssl polyfill returns garbage. (nielsdos)
5959
. Fix file descriptor leak in phar_zip_flush() on failure. (nielsdos)
60+
. Fix memory leak when opening temp file fails while trying to open
61+
gzip-compressed archive. (nielsdos)
6062

6163
- Random:
6264
. Fix Randomizer::__serialize() w.r.t. INDIRECTs. (nielsdos)

ext/phar/phar.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,18 +1672,19 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
16721672
if (!PHAR_G(has_zlib)) {
16731673
MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file, enable zlib extension in php.ini")
16741674
}
1675+
1676+
/* entire file is gzip-compressed, uncompress to temporary file */
1677+
if (!(temp = php_stream_fopen_tmpfile())) {
1678+
MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of gzipped phar archive \"%s\"")
1679+
}
1680+
16751681
array_init(&filterparams);
16761682
/* this is defined in zlib's zconf.h */
16771683
#ifndef MAX_WBITS
16781684
#define MAX_WBITS 15
16791685
#endif
16801686
add_assoc_long_ex(&filterparams, "window", sizeof("window") - 1, MAX_WBITS + 32);
16811687

1682-
/* entire file is gzip-compressed, uncompress to temporary file */
1683-
if (!(temp = php_stream_fopen_tmpfile())) {
1684-
MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of gzipped phar archive \"%s\"")
1685-
}
1686-
16871688
php_stream_rewind(fp);
16881689
filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp));
16891690

0 commit comments

Comments
 (0)