From f3f9f52af9a8d2ee7e94892b65020d9c6a1ccd18 Mon Sep 17 00:00:00 2001 From: Greg Beaver Date: Mon, 14 Apr 2008 17:18:58 +0000 Subject: [PATCH] minor re-factoring of phar_open_url to remove one nested brackets, put fopen edge cases in its own test, tweak test phar file names --- ext/phar/stream.c | 125 ++++++++++---------- ext/phar/tests/fopen_edgecases.phpt | 43 +++++++ ext/phar/tests/open_for_write_existing.phpt | 5 - ext/phar/tests/phar_dir_iterate.phpt | 13 +- 4 files changed, 112 insertions(+), 74 deletions(-) create mode 100644 ext/phar/tests/fopen_edgecases.phpt diff --git a/ext/phar/stream.c b/ext/phar/stream.c index 0d73908d53..43937f4c01 100644 --- a/ext/phar/stream.c +++ b/ext/phar/stream.c @@ -63,35 +63,37 @@ php_url* phar_open_url(php_stream_wrapper *wrapper, char *filename, char *mode, char *arch = NULL, *entry = NULL, *error; int arch_len, entry_len; - if (!strncasecmp(filename, "phar://", 7)) { - if (mode[0] == 'a') { - if (!(options & PHP_STREAM_URL_STAT_QUIET)) { - php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: open mode append not supported"); - } - return NULL; - } - if (phar_split_fname(filename, strlen(filename), &arch, &arch_len, &entry, &entry_len TSRMLS_CC) == FAILURE) { - if (!(options & PHP_STREAM_URL_STAT_QUIET)) { - if (arch && !entry) { - php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: no directory in \"%s\", must have at least phar://%s/ for root directory (always use full path to a new phar)", filename, arch); - arch = NULL; - } else { - php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: invalid url \"%s\" (cannot contain .phar.php and .phar.gz/.phar.bz2)", filename); - } - } - if (arch) { - efree(arch); - } - if (entry) { - efree(entry); + if (strlen(filename) < 7 || strncasecmp(filename, "phar://", 7)) { + return NULL; + } + if (mode[0] == 'a') { + if (!(options & PHP_STREAM_URL_STAT_QUIET)) { + php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: open mode append not supported"); + } + return NULL; + } + if (phar_split_fname(filename, strlen(filename), &arch, &arch_len, &entry, &entry_len TSRMLS_CC) == FAILURE) { + if (!(options & PHP_STREAM_URL_STAT_QUIET)) { + if (arch && !entry) { + php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: no directory in \"%s\", must have at least phar://%s/ for root directory (always use full path to a new phar)", filename, arch); + arch = NULL; + } else { + php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: invalid url \"%s\" (cannot contain .phar.php and .phar.gz/.phar.bz2)", filename); } - return NULL; } - resource = ecalloc(1, sizeof(php_url)); - resource->scheme = estrndup("phar", 4); - resource->host = arch; + if (arch) { + efree(arch); + } + if (entry) { + efree(entry); + } + return NULL; + } + resource = ecalloc(1, sizeof(php_url)); + resource->scheme = estrndup("phar", 4); + resource->host = arch; - resource->path = entry; + resource->path = entry; #if MBO_0 if (resource) { fprintf(stderr, "Alias: %s\n", alias); @@ -105,50 +107,47 @@ php_url* phar_open_url(php_stream_wrapper *wrapper, char *filename, char *mode, /* fprintf(stderr, "Fragment: %s\n", resource->fragment);*/ } #endif - if (PHAR_GLOBALS->request_init && PHAR_GLOBALS->phar_plain_map.arBuckets && zend_hash_exists(&(PHAR_GLOBALS->phar_plain_map), arch, arch_len+1)) { - return resource; - } - if (mode[0] == 'w' || (mode[0] == 'r' && mode[1] == '+')) { - phar_archive_data **pphar = NULL; + if (PHAR_GLOBALS->request_init && PHAR_GLOBALS->phar_plain_map.arBuckets && zend_hash_exists(&(PHAR_GLOBALS->phar_plain_map), arch, arch_len+1)) { + return resource; + } + if (mode[0] == 'w' || (mode[0] == 'r' && mode[1] == '+')) { + phar_archive_data **pphar = NULL; - if (PHAR_GLOBALS->request_init && PHAR_GLOBALS->phar_fname_map.arBuckets && FAILURE == zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), arch, arch_len, (void **)&pphar)) { - pphar = NULL; + if (PHAR_GLOBALS->request_init && PHAR_GLOBALS->phar_fname_map.arBuckets && FAILURE == zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), arch, arch_len, (void **)&pphar)) { + pphar = NULL; + } + if (PHAR_G(readonly) && (!pphar || !(*pphar)->is_data)) { + if (!(options & PHP_STREAM_URL_STAT_QUIET)) { + php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: write operations disabled by INI setting"); } - if (PHAR_G(readonly) && (!pphar || !(*pphar)->is_data)) { + php_url_free(resource); + return NULL; + } + if (phar_open_or_create_filename(resource->host, arch_len, NULL, 0, NULL, options, NULL, &error TSRMLS_CC) == FAILURE) + { + if (error) { if (!(options & PHP_STREAM_URL_STAT_QUIET)) { - php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, "phar error: write operations disabled by INI setting"); - } - php_url_free(resource); - return NULL; - } - if (phar_open_or_create_filename(resource->host, arch_len, NULL, 0, NULL, options, NULL, &error TSRMLS_CC) == FAILURE) - { - if (error) { - if (!(options & PHP_STREAM_URL_STAT_QUIET)) { - php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, error); - } - efree(error); + php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, error); } - php_url_free(resource); - return NULL; + efree(error); } - } else { - if (phar_open_filename(resource->host, arch_len, NULL, 0, options, NULL, &error TSRMLS_CC) == FAILURE) - { - if (error) { - if (!(options & PHP_STREAM_URL_STAT_QUIET)) { - php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, error); - } - efree(error); + php_url_free(resource); + return NULL; + } + } else { + if (phar_open_filename(resource->host, arch_len, NULL, 0, options, NULL, &error TSRMLS_CC) == FAILURE) + { + if (error) { + if (!(options & PHP_STREAM_URL_STAT_QUIET)) { + php_stream_wrapper_log_error(wrapper, options TSRMLS_CC, error); } - php_url_free(resource); - return NULL; + efree(error); } - } - return resource; - } - - return NULL; + php_url_free(resource); + return NULL; + } + } + return resource; } /* }}} */ diff --git a/ext/phar/tests/fopen_edgecases.phpt b/ext/phar/tests/fopen_edgecases.phpt new file mode 100644 index 0000000000..33819847e5 --- /dev/null +++ b/ext/phar/tests/fopen_edgecases.phpt @@ -0,0 +1,43 @@ +--TEST-- +Phar: fopen/stat edge cases +--SKIPIF-- + +--INI-- +phar.readonly=0 +phar.require_hash=0 +--FILE-- + + +===DONE=== +--CLEAN-- + + +--EXPECTF-- +Warning: fopen(phar://%sfopen_edgecases.phar.php/b/c.php): failed to open stream: phar error: open mode append not supported in %sfopen_edgecases.php on line %d + +Warning: fopen(phar://%sfopen_edgecases.phar.php.phar.gz): failed to open stream: phar error: invalid url "phar://%sfopen_edgecases.phar.php.phar.gz" (cannot contain .phar.php and .phar.gz/.phar.bz2) in %sfopen_edgecases.php on line %d +bool(false) + +Warning: fopen(phar://%sfopen_edgecases.2.phar.php/hi): failed to open stream: internal corruption of phar "%sfopen_edgecases.2.phar.php" (truncated manifest at stub end) in %sfopen_edgecases.php on line %d + +===DONE=== \ No newline at end of file diff --git a/ext/phar/tests/open_for_write_existing.phpt b/ext/phar/tests/open_for_write_existing.phpt index a3313b80ef..2a3ec8b824 100644 --- a/ext/phar/tests/open_for_write_existing.phpt +++ b/ext/phar/tests/open_for_write_existing.phpt @@ -21,9 +21,6 @@ $fp = fopen($pname . '/b/c.php', 'wb'); fwrite($fp, 'extra'); fclose($fp); include $pname . '/b/c.php'; - -// add edge case test for append -$a = fopen($pname . '/b/c.php', 'a'); ?> ===DONE=== @@ -31,6 +28,4 @@ $a = fopen($pname . '/b/c.php', 'a'); --EXPECTF-- extra -Warning: fopen(phar://%sopen_for_write_existing.phar.php/b/c.php): failed to open stream: phar error: open mode append not supported in %sopen_for_write_existing.php on line %d - ===DONE=== diff --git a/ext/phar/tests/phar_dir_iterate.phpt b/ext/phar/tests/phar_dir_iterate.phpt index 1ca8639eae..2ee2cb5529 100644 --- a/ext/phar/tests/phar_dir_iterate.phpt +++ b/ext/phar/tests/phar_dir_iterate.phpt @@ -8,12 +8,13 @@ phar.readonly=0 phar.require_hash=0 --FILE-- $obj) { var_dump($obj->getPathName()); } @@ -21,11 +22,11 @@ foreach (new RecursiveIteratorIterator($newphar) as $path => $obj) { ===DONE=== --CLEAN-- --EXPECTF-- -string(%d) "phar://%stest.phar%canother.file.txt" -string(%d) "phar://%stest.phar%csub%ctop.txt" -string(%d) "phar://%stest.phar%ctop.txt" +string(%d) "phar://%sphar_dir_iterate.phar.php%canother.file.txt" +string(%d) "phar://%sphar_dir_iterate.phar.php%csub%ctop.txt" +string(%d) "phar://%sphar_dir_iterate.phar.php%ctop.txt" ===DONE=== -- 2.50.1