Skip to content

Commit c77f783

Browse files
committed
Fix #70322: ZipArchive::close() doesn't indicate errors
If an archive can't be written, ZipArchive::close() nonetheless returns TRUE. We fix the return value to properly return success, and additionally raise a warning on failure.
1 parent 4b1dff6 commit c77f783

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

ext/zip/php_zip.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,7 @@ static ZIPARCHIVE_METHOD(close)
16161616
struct zip *intern;
16171617
zval *this = getThis();
16181618
ze_zip_object *ze_obj;
1619+
int err;
16191620

16201621
if (!this) {
16211622
RETURN_FALSE;
@@ -1625,7 +1626,8 @@ static ZIPARCHIVE_METHOD(close)
16251626

16261627
ze_obj = (ze_zip_object*) zend_object_store_get_object(this TSRMLS_CC);
16271628

1628-
if (zip_close(intern)) {
1629+
if (err = zip_close(intern)) {
1630+
php_error_docref(NULL TSRMLS_CC, E_WARNING, zip_strerror(intern));
16291631
zip_discard(intern);
16301632
}
16311633

@@ -1634,7 +1636,11 @@ static ZIPARCHIVE_METHOD(close)
16341636
ze_obj->filename_len = 0;
16351637
ze_obj->za = NULL;
16361638

1637-
RETURN_TRUE;
1639+
if (!err) {
1640+
RETURN_TRUE;
1641+
} else {
1642+
RETURN_FALSE;
1643+
}
16381644
}
16391645
/* }}} */
16401646

ext/zip/tests/bug70322.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Bug #70322 (ZipArchive::close() doesn't indicate errors)
3+
--DESCRIPTION--
4+
We want to test whether ZipArchive::close() returns FALSE and raises a warning
5+
on failure, so we force the failure by adding a file to the archive, which we
6+
delete before closing.
7+
--SKIPIF--
8+
<?php
9+
if (!extension_loaded('zip')) die('skip requires zip extension');
10+
?>
11+
--FILE--
12+
<?php
13+
$zipfile = __DIR__ . '/bug70322.zip';
14+
$textfile = __DIR__ . '/bug70322.txt';
15+
touch($textfile);
16+
$zip = new ZipArchive();
17+
$zip->open($zipfile, ZipArchive::CREATE);
18+
$zip->addFile($textfile);
19+
unlink($textfile);
20+
var_dump($zip->close());
21+
?>
22+
--CLEAN--
23+
<?php
24+
// we don't expect the archive to be created, but clean up just in case...
25+
@unlink(__DIR__ . '/bug70322.zip');
26+
?>
27+
--EXPECTF--
28+
Warning: ZipArchive::close(): Read error: No such file or directory in %s%ebug70322.php on line %d
29+
bool(false)

ext/zip/tests/oo_delete.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ $sb = $zip->statIndex(1);
6363
var_dump($sb);
6464
$sb = $zip->statIndex(2);
6565
var_dump($sb);
66-
$zip->close();
66+
// suppress irrelevant error message:
67+
@$zip->close();
6768
unset($zip);
6869

6970
if (file_exists($file)) {

0 commit comments

Comments
 (0)