From c4bff4d756ba9f279dd33ae7da2ec3694e85c46e Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Sun, 3 Jun 2007 21:30:12 +0000 Subject: [PATCH] - MFB: PECL #11216, addEmptyDir crashes if the directory already exists --- ext/zip/php_zip.c | 34 +++++++++++++++++++++++++++++++--- ext/zip/tests/bug11216.phpt | 28 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 ext/zip/tests/bug11216.phpt diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c index 8a4497ea39..ed9a22ad52 100644 --- a/ext/zip/php_zip.c +++ b/ext/zip/php_zip.c @@ -996,6 +996,9 @@ static ZIPARCHIVE_METHOD(addEmptyDir) zval *this = getThis(); char *dirname; int dirname_len; + int idx; + struct zip_stat sb; + char *s; if (!this) { RETURN_FALSE; @@ -1007,14 +1010,39 @@ static ZIPARCHIVE_METHOD(addEmptyDir) &dirname, &dirname_len, UG(ascii_conv)) == FAILURE) { return; } + if (dirname_len<1) { RETURN_FALSE; } - if (zip_add_dir(intern, (const char *)dirname) < 0) { - RETURN_FALSE; + if (dirname[dirname_len-1] != '/') { + s=(char *)emalloc(dirname_len+2); + strcpy(s, dirname); + s[dirname_len] = '/'; + s[dirname_len+1] = '\0'; + } else { + s = dirname; + } + + idx = zip_stat(intern, s, 0, &sb); + if (idx >= 0) { + RETVAL_FALSE; + } else { + /* reset the error */ + if (intern->error.str) { + _zip_error_fini(&intern->error); + } + _zip_error_init(&intern->error); + + if (zip_add_dir(intern, (const char *)s) == -1) { + RETVAL_FALSE; + } + RETVAL_TRUE; + } + + if (s != dirname) { + efree(s); } - RETURN_TRUE; } /* }}} */ diff --git a/ext/zip/tests/bug11216.phpt b/ext/zip/tests/bug11216.phpt new file mode 100644 index 0000000000..607217ad72 --- /dev/null +++ b/ext/zip/tests/bug11216.phpt @@ -0,0 +1,28 @@ +--TEST-- +Bug #11216 (::addEmptyDir() crashes when the directory already exists) +--SKIPIF-- + +--FILE-- +open('__test.zip', ZIPARCHIVE::CREATE); +var_dump($archive->addEmptyDir('test')); +print_r($archive); +var_dump($archive->addEmptyDir('test')); +$archive->close(); +unlink('__test.zip'); +?> +--EXPECT-- +bool(true) +ZipArchive Object +( + [status] => 0 + [statusSys] => 0 + [numFiles] => 1 + [filename] => + [comment] => +) +bool(false) -- 2.50.1