From fe1fd6120db6d03c1a580a21b32e059fbc93d7aa Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Thu, 19 Mar 2020 17:31:17 +0100 Subject: [PATCH] Fix Bug #79296 ZipArchive::open fails on empty file --- ext/zip/php_zip.c | 15 +++++++++++++++ ext/zip/tests/bug53885.phpt | 1 - 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c index f9a9a8d74f4e8..fb9d3aefec889 100644 --- a/ext/zip/php_zip.c +++ b/ext/zip/php_zip.c @@ -1487,6 +1487,21 @@ static ZIPARCHIVE_METHOD(open) ze_obj->filename = NULL; } +#if LIBZIP_VERSION_MAJOR > 1 || LIBZIP_VERSION_MAJOR == 1 && LIBZIP_VERSION_MINOR >= 6 + /* reduce BC break introduce in libzip 1.6.0 + "Do not accept empty files as valid zip archives any longer" */ + + /* open for write without option to empty the archive */ + if ((flags & (ZIP_TRUNCATE | ZIP_RDONLY)) == 0) { + zend_stat_t st; + + /* exists and is empty */ + if (VCWD_STAT(resolved_path, &st) == 0 && st.st_size == 0) { + flags |= ZIP_TRUNCATE; + } + } +#endif + intern = zip_open(resolved_path, flags, &err); if (!intern || err) { efree(resolved_path); diff --git a/ext/zip/tests/bug53885.phpt b/ext/zip/tests/bug53885.phpt index 75f6fe1609eaa..6f95a661b323b 100644 --- a/ext/zip/tests/bug53885.phpt +++ b/ext/zip/tests/bug53885.phpt @@ -3,7 +3,6 @@ Bug #53885 (ZipArchive segfault with FL_UNCHANGED on empty archive) --SKIPIF-- =')) die('skip libzip too recent'); ?> --FILE--