]> granicus.if.org Git - php/commitdiff
Cleanup (avoid reallocation)
authorDmitry Stogov <dmitry@zend.com>
Thu, 2 Jul 2015 17:29:06 +0000 (20:29 +0300)
committerDmitry Stogov <dmitry@zend.com>
Thu, 2 Jul 2015 17:29:06 +0000 (20:29 +0300)
ext/phar/phar.c
ext/phar/phar_internal.h
ext/phar/phar_object.c
ext/phar/stub.h

index 4f40153ab0b67b703998d42d38e34aca0b1bbd5c..9956effb3f501c0b012b1621b4ca4251bf71473e 100644 (file)
@@ -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);
index ef752817aab675a2e9bc6d78a829e9772a282df3..f5209faf7557a9551ec1177cb878fed53d478ddd 100644 (file)
@@ -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);
 
index 1cfaee416f17ba04b178dd8a3e2df10dfab902ac..f93738f60cda6cc4a4dbf8f1810d2c224882f8df 100644 (file)
@@ -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) {
index 313787f7047e9111aa6991a80a5c6612ba86d886..db1940bdec0a7327b22866990fdc805fb279de22 100644 (file)
@@ -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[] = "<?php\n\n$web = '";
        static const char newstub1_0[] = "';\n\nif (in_array('phar', stream_get_wrappers()) && class_exists('Phar', 0)) {\nPhar::interceptFileFuncs();\nset_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());\nPhar::webPhar(null, $web);\ninclude 'phar://' . __FILE__ . '/' . Extract_Phar::START;\nreturn;\n}\n\nif (@(isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'POST'))) {\nExtract_Phar::go(true);\n$mimes = array(\n'phps' => 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);
 }