[php-src] PHP-8.3: phar: Fix memory leak when opening temp file fails while trying to open gzip-compressed archive

From: Date: Tue, 21 Oct 2025 18:04:54 +0000
Subject: [php-src] PHP-8.3: phar: Fix memory leak when opening temp file fails while trying to open gzip-compressed archive
Groups: php.cvs 
Request: Send a blank email to [email protected] to get a copy of this message
Author: Niels Dossche (nielsdos)
Date: 2025-10-21T20:04:13+02:00

Commit: https://github.com/php/php-src/commit/ce0df1a9d82dbb3166a889327ebb1c59a640f95f
Raw diff: https://github.com/php/php-src/commit/ce0df1a9d82dbb3166a889327ebb1c59a640f95f.diff

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 GH-20220.

Changed paths:
  M  NEWS
  M  ext/phar/phar.c


Diff:

diff --git a/NEWS b/NEWS
index 4345f86f98ec..27214db0b90e 100644
--- a/NEWS
+++ b/NEWS
@@ -57,6 +57,8 @@ PHP                                                                        NEWS
     of type size_t. (Girgias)
   . Fix memory leak when openssl polyfill returns garbage. (nielsdos)
   . Fix file descriptor leak in phar_zip_flush() on failure. (nielsdos)
+  . Fix memory leak when opening temp file fails while trying to open
+    gzip-compressed archive. (nielsdos)
 
 - Random:
   . Fix Randomizer::__serialize() w.r.t. INDIRECTs. (nielsdos)
diff --git a/ext/phar/phar.c b/ext/phar/phar.c
index b7baf9e69ce4..a9aff9489df0 100644
--- a/ext/phar/phar.c
+++ b/ext/phar/phar.c
@@ -1672,6 +1672,12 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len,
char
 				if (!PHAR_G(has_zlib)) {
 					MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to
temporary file, enable zlib extension in php.ini")
 				}
+
+				/* entire file is gzip-compressed, uncompress to temporary file */
+				if (!(temp = php_stream_fopen_tmpfile())) {
+					MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of gzipped phar
archive \"%s\"")
+				}
+
 				array_init(&filterparams);
 /* this is defined in zlib's zconf.h */
 #ifndef MAX_WBITS
@@ -1679,11 +1685,6 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len,
char
 #endif
 				add_assoc_long_ex(&filterparams, "window", sizeof("window") - 1,
MAX_WBITS + 32);
 
-				/* entire file is gzip-compressed, uncompress to temporary file */
-				if (!(temp = php_stream_fopen_tmpfile())) {
-					MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of gzipped phar
archive \"%s\"")
-				}
-
 				php_stream_rewind(fp);
 				filter = php_stream_filter_create("zlib.inflate", &filterparams,
php_stream_is_persistent(fp));
 


Thread (1 message)

  • Niels Dossche
« previous php.cvs (#136499) next »