From b874f1a73d37f17effe7b85042fef0dc703448f8 Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Thu, 2 Jul 2015 20:29:06 +0300 Subject: [PATCH] Cleanup (avoid reallocation) --- ext/phar/phar.c | 23 +++++++++-------------- ext/phar/phar_internal.h | 2 +- ext/phar/phar_object.c | 22 ++++++++++------------ ext/phar/stub.h | 4 ++-- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/ext/phar/phar.c b/ext/phar/phar.c index 4f40153ab0..9956effb3f 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -2430,15 +2430,9 @@ static int phar_flush_clean_deleted_apply(zval *zv) /* {{{ */ #include "stub.h" -char *phar_create_default_stub(const char *index_php, const char *web_index, size_t *len, char **error) /* {{{ */ +zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error) /* {{{ */ { - char *stub = NULL; int index_len, web_len; - size_t dummy; - - if (!len) { - len = &dummy; - } if (error) { *error = NULL; @@ -2471,8 +2465,7 @@ char *phar_create_default_stub(const char *index_php, const char *web_index, siz } } - phar_get_stub(index_php, web_index, len, &stub, index_len+1, web_len+1); - return stub; + return phar_get_stub(index_php, web_index, index_len+1, web_len+1); } /* }}} */ @@ -2485,7 +2478,8 @@ char *phar_create_default_stub(const char *index_php, const char *web_index, siz int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int convert, char **error) /* {{{ */ { char halt_stub[] = "__HALT_COMPILER();"; - char *newstub, *tmp; + zend_string *newstub; + char *tmp; phar_entry_info *entry, *newentry; int halt_offset, restore_alias_len, global_flags = 0, closeoldfile; char *pos, has_dirs = 0; @@ -2631,8 +2625,9 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv newstub = NULL; } else { /* this is either a brand new phar or a default stub overwrite */ - newstub = phar_create_default_stub(NULL, NULL, &(phar->halt_offset), NULL); - written = php_stream_write(newfile, newstub, phar->halt_offset); + newstub = phar_create_default_stub(NULL, NULL, NULL); + phar->halt_offset = ZSTR_LEN(newstub); + written = php_stream_write(newfile, ZSTR_VAL(newstub), phar->halt_offset); } if (phar->halt_offset != written) { if (closeoldfile) { @@ -2647,12 +2642,12 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv } } if (newstub) { - efree(newstub); + zend_string_free(newstub); } return EOF; } if (newstub) { - efree(newstub); + zend_string_free(newstub); } } manifest_ftell = php_stream_tell(newfile); diff --git a/ext/phar/phar_internal.h b/ext/phar/phar_internal.h index ef752817aa..f5209faf75 100644 --- a/ext/phar/phar_internal.h +++ b/ext/phar/phar_internal.h @@ -558,7 +558,7 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, php_uint32 sig_typ int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, int *signature_length, char **error); /* utility functions */ -char *phar_create_default_stub(const char *index_php, const char *web_index, size_t *len, char **error); +zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error); char *phar_decompress_filter(phar_entry_info * entry, int return_unknown); char *phar_compress_filter(phar_entry_info * entry, int return_unknown); diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 1cfaee416f..f93738f60c 100644 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -934,24 +934,22 @@ PHP_METHOD(Phar, interceptFileFuncs) */ PHP_METHOD(Phar, createDefaultStub) { - char *index = NULL, *webindex = NULL, *stub, *error; + char *index = NULL, *webindex = NULL, *error; + zend_string *stub; size_t index_len = 0, webindex_len = 0; - size_t stub_len; if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ss", &index, &index_len, &webindex, &webindex_len) == FAILURE) { return; } - stub = phar_create_default_stub(index, webindex, &stub_len, &error); + stub = phar_create_default_stub(index, webindex, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); efree(error); return; } - // TODO: avoid reallocation ??? - RETVAL_STRINGL(stub, stub_len); - efree(stub); + RETURN_NEW_STR(stub); } /* }}} */ @@ -2909,10 +2907,10 @@ PHP_METHOD(Phar, setStub) */ PHP_METHOD(Phar, setDefaultStub) { - char *index = NULL, *webindex = NULL, *error = NULL, *stub = NULL; + char *index = NULL, *webindex = NULL, *error = NULL; + zend_string *stub = NULL; size_t index_len = 0, webindex_len = 0; int created_stub = 0; - size_t stub_len = 0; PHAR_ARCHIVE_OBJECT(); if (phar_obj->archive->is_data) { @@ -2942,13 +2940,13 @@ PHP_METHOD(Phar, setDefaultStub) } if (!phar_obj->archive->is_tar && !phar_obj->archive->is_zip) { - stub = phar_create_default_stub(index, webindex, &stub_len, &error); + stub = phar_create_default_stub(index, webindex, &error); if (error) { zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "%s", error); efree(error); if (stub) { - efree(stub); + zend_string_free(stub); } RETURN_FALSE; } @@ -2960,10 +2958,10 @@ PHP_METHOD(Phar, setDefaultStub) zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname); return; } - phar_flush(phar_obj->archive, stub, stub_len, 1, &error); + phar_flush(phar_obj->archive, stub ? ZSTR_VAL(stub) : 0, stub ? ZSTR_LEN(stub) : 0, 1, &error); if (created_stub) { - efree(stub); + zend_string_free(stub); } if (error) { diff --git a/ext/phar/stub.h b/ext/phar/stub.h index 313787f704..db1940bdec 100644 --- a/ext/phar/stub.h +++ b/ext/phar/stub.h @@ -18,7 +18,7 @@ /* $Id$ */ -static inline void phar_get_stub(const char *index_php, const char *web, size_t *len, char **stub, const int name_len, const int web_len) +static inline zend_string* phar_get_stub(const char *index_php, const char *web, const int name_len, const int web_len) { static const char newstub0[] = " 2,\n'c' => 'text/plain',\n'cc' => 'text/plain',\n'cpp' => 'text/plain',\n'c++' => 'text/plain',\n'dtd' => 'text/plain',\n'h' => 'text/plain',\n'log' => 'text/plain',\n'rng' => 'text/plain',\n'txt' => 'text/plain',\n'xsd' => 'text/plain',\n'php' => 1,\n'inc' => 1,\n'avi' => 'video/avi',\n'bmp' => 'image/bmp',\n'css' => 'text/css',\n'gif' => 'image/gif',\n'htm' => 'text/html',\n'html' => 'text/html',\n'htmls' => 'text/html',\n'ico' => 'image/x-ico',\n'jpe' => 'image/jpeg',\n'jpg' => 'image/jpeg',\n'jpeg' => 'image/jpeg',\n'js' => 'application/x-javascript',\n'midi' => 'audio/midi',\n'mid' => 'audio/midi',\n'mod' => 'audio/mod',\n'mov' => 'movie/quicktime',\n'mp3' => 'audio/mp3',\n'mpg' => 'video/mpeg',\n'mpeg' => 'video/mpeg',\n'pdf' => 'application/pdf',\n'png' => 'image/png',\n'swf' => 'application/shockwave-flash',\n'tif' => 'image/tiff',\n'tiff' => 'image/tiff',\n'wav' => 'audio/wav',\n'xbm' => 'image/xbm',\n'xml' => 'text/xml',\n);\n\nheader(\"Cache-Control: no-cache, must-revalidate\");\nheader(\"Pragma: no-cache\");\n\n$basename = basename(__FILE__);\nif (!strpos($_SERVER['REQUEST_URI'], $basename)) {\nchdir(Extract_Phar::$temp);\ninclude $web;\nreturn;\n}\n$pt = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $basename) + strlen($basename));\nif (!$pt || $pt == '/') {\n$pt = $web;\nheader('HTTP/1.1 301 Moved Permanently');\nheader('Location: ' . $_SERVER['REQUEST_URI'] . '/' . $pt);\nexit;\n}\n$a = realpath(Extract_Phar::$temp . DIRECTORY_SEPARATOR . $pt);\nif (!$a || strlen(dirname($a)) < strlen("; @@ -30,5 +30,5 @@ static inline void phar_get_stub(const char *index_php, const char *web, size_t static const int newstub_len = 6665; - *len = spprintf(stub, name_len + web_len + newstub_len, "%s%s%s%s%s%s%d%s%s%s", newstub0, web, newstub1_0, newstub1_1, index_php, newstub2, name_len + web_len + newstub_len, newstub3_0, newstub3_1, newstub3_2); + return strpprintf(name_len + web_len + newstub_len, "%s%s%s%s%s%s%d%s%s%s", newstub0, web, newstub1_0, newstub1_1, index_php, newstub2, name_len + web_len + newstub_len, newstub3_0, newstub3_1, newstub3_2); } -- 2.50.1