From: Pierre Joye Date: Mon, 29 Jan 2007 15:25:06 +0000 (+0000) Subject: - #40228, ZipArchive::extractTo does create empty directories X-Git-Tag: php-5.2.1~27 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=601a77b3a38bb3bc9af731e2742fcc1558f7f9e9;p=php - #40228, ZipArchive::extractTo does create empty directories recursively --- diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c index 4f63d74d53..745bcf6082 100644 --- a/ext/zip/php_zip.c +++ b/ext/zip/php_zip.c @@ -106,27 +106,32 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil char *file_basename; size_t file_basename_len; + int is_dir_only = 0; if (file_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb)) { return 0; } - memcpy(file_dirname, file, file_len); - - dir_len = php_dirname(file_dirname, file_len); - - if (dir_len > 0) { - len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file_dirname); + if (file_len > 1 && file[file_len - 1] == '/') { + len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file); + is_dir_only = 1; } else { - len = spprintf(&file_dirname_fullpath, 0, "%s", dest); - } + memcpy(file_dirname, file, file_len); + dir_len = php_dirname(file_dirname, file_len); - php_basename(file, file_len, NULL, 0, &file_basename, &file_basename_len TSRMLS_CC); + if (dir_len > 0) { + len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file_dirname); + } else { + len = spprintf(&file_dirname_fullpath, 0, "%s", dest); + } - if (SAFEMODE_CHECKFILE(file_dirname_fullpath)) { - efree(file_dirname_fullpath); - efree(file_basename); - return 0; + php_basename(file, file_len, NULL, 0, &file_basename, (unsigned int *)&file_basename_len TSRMLS_CC); + + if (SAFEMODE_CHECKFILE(file_dirname_fullpath)) { + efree(file_dirname_fullpath); + efree(file_basename); + return 0; + } } /* let see if the path already exists */ @@ -142,7 +147,9 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil /* it is a standalone directory, job done */ if (file[file_len - 1] == '/') { efree(file_dirname_fullpath); - efree(file_basename); + if (!is_dir_only) { + efree(file_basename); + } return 1; } diff --git a/ext/zip/tests/bug40228.phpt b/ext/zip/tests/bug40228.phpt new file mode 100644 index 0000000000..fec2963639 --- /dev/null +++ b/ext/zip/tests/bug40228.phpt @@ -0,0 +1,23 @@ +--TEST-- +Bug #40228 (extractTo does not create recursive empty path) +--SKIPIF-- + +--FILE-- +open($arc_name, ZIPARCHIVE::CREATE);; +$zip->extractTo($dest); +if (is_dir($dest . '/test/empty')) { + echo "Ok\n"; + rmdir($dest . '/test/empty'); + rmdir($dest . '/test'); +} else { + echo "Failed.\n"; +} +echo "Done\n"; +?> +--EXPECT-- +Ok +Done diff --git a/ext/zip/tests/bug40228.zip b/ext/zip/tests/bug40228.zip new file mode 100644 index 0000000000..bbcd9515f8 Binary files /dev/null and b/ext/zip/tests/bug40228.zip differ