Skip to content

Commit 80860ba

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0: Fix #81490: ZipArchive::extractTo() may leak memory
2 parents 02244d5 + 5db6e35 commit 80860ba

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ PHP NEWS
1111
. Fixed bug #81475 (stream_isatty emits warning with attached stream wrapper).
1212
(cmb)
1313

14+
- Zip:
15+
. Fixed bug #81490 (ZipArchive::extractTo() may leak memory). (cmb, Remi)
16+
1417
30 Sep 2021, PHP 8.1.0RC3
1518

1619
- Filter:

ext/zip/php_zip.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
149149
virtual_file_ex(&new_state, file, NULL, CWD_EXPAND);
150150
path_cleaned = php_zip_make_relative_path(new_state.cwd, new_state.cwd_length);
151151
if(!path_cleaned) {
152+
CWD_STATE_FREE(new_state.cwd);
152153
return 0;
153154
}
154155
path_cleaned_len = strlen(path_cleaned);
155156

156157
if (path_cleaned_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb) != 0) {
158+
CWD_STATE_FREE(new_state.cwd);
157159
return 0;
158160
}
159161

@@ -188,8 +190,8 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
188190
efree(file_dirname_fullpath);
189191
if (!is_dir_only) {
190192
zend_string_release_ex(file_basename, 0);
191-
CWD_STATE_FREE(new_state.cwd);
192193
}
194+
CWD_STATE_FREE(new_state.cwd);
193195
return 0;
194196
}
195197
}

ext/zip/tests/bug81490.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #81490 (ZipArchive::extractTo() may leak memory)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("zip")) die("skip zip extension not available");
6+
?>
7+
--FILE--
8+
<?php
9+
$zip = new ZipArchive();
10+
$zip->open(__DIR__ . "/bug81490.zip", ZipArchive::CREATE|ZipArchive::OVERWRITE);
11+
$zip->addFromString("", "yada yada");
12+
mkdir(__DIR__ . "/bug81490");
13+
$zip->open(__DIR__ . "/bug81490.zip");
14+
$zip->extractTo(__DIR__ . "/bug81490", "");
15+
?>
16+
--EXPECT--
17+
--CLEAN--
18+
<?php
19+
@unlink(__DIR__ . "/bug81490.zip");
20+
@rmdir(__DIR__ . "/bug81490");
21+
?>

0 commit comments

Comments
 (0)