Skip to content

Commit 8aad313

Browse files
committed
Fix #70752: Depacking with wrong password leaves 0 length files
We should not open the output stream before we have tried to open the archive entry, as failing the latter could leave an empty file behind.
1 parent dd6da58 commit 8aad313

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ PHP NEWS
1717
. Fixed bug #72590 (Opcache restart with kill_all_lockers does not work).
1818
(Keyur) (julien backport)
1919

20+
- Zip:
21+
. Fixed bug #70752 (Depacking with wrong password leaves 0 length files).
22+
(cmb)
23+
2024
15 Sep 2016, PHP 5.6.26
2125

2226
- Core:

ext/zip/php_zip.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
279279
return 0;
280280
}
281281

282+
zf = zip_fopen(za, file, 0);
283+
if (zf == NULL) {
284+
n = -1;
285+
goto done;
286+
}
287+
282288
#if PHP_API_VERSION < 20100412
283289
stream = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
284290
#else
@@ -287,13 +293,7 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
287293

288294
if (stream == NULL) {
289295
n = -1;
290-
goto done;
291-
}
292-
293-
zf = zip_fopen(za, file, 0);
294-
if (zf == NULL) {
295-
n = -1;
296-
php_stream_close(stream);
296+
zip_fclose(zf);
297297
goto done;
298298
}
299299

ext/zip/tests/bug70752.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Bug #70752 (Depacking with wrong password leaves 0 length files)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('zip')) die('skip zip extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'bug70752.zip';
10+
$zip = new ZipArchive();
11+
$zip->open($filename);
12+
13+
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'bug70752.txt';
14+
var_dump(file_exists($filename));
15+
16+
$zip->setPassword('bar'); // correct password would be 'foo'
17+
$zip->extractTo(__DIR__);
18+
$zip->close();
19+
20+
var_dump(file_exists($filename));
21+
?>
22+
===DONE===
23+
--EXPECT--
24+
bool(false)
25+
bool(false)
26+
===DONE===
27+
--CLEAN--
28+
<?php
29+
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'bug70752.txt';
30+
unlink($filename);
31+
?>

ext/zip/tests/bug70752.zip

175 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)