Skip to content

Commit b874f1a

Browse files
committed
Cleanup (avoid reallocation)
1 parent 07e646f commit b874f1a

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

ext/phar/phar.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,15 +2430,9 @@ static int phar_flush_clean_deleted_apply(zval *zv) /* {{{ */
24302430

24312431
#include "stub.h"
24322432

2433-
char *phar_create_default_stub(const char *index_php, const char *web_index, size_t *len, char **error) /* {{{ */
2433+
zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error) /* {{{ */
24342434
{
2435-
char *stub = NULL;
24362435
int index_len, web_len;
2437-
size_t dummy;
2438-
2439-
if (!len) {
2440-
len = &dummy;
2441-
}
24422436

24432437
if (error) {
24442438
*error = NULL;
@@ -2471,8 +2465,7 @@ char *phar_create_default_stub(const char *index_php, const char *web_index, siz
24712465
}
24722466
}
24732467

2474-
phar_get_stub(index_php, web_index, len, &stub, index_len+1, web_len+1);
2475-
return stub;
2468+
return phar_get_stub(index_php, web_index, index_len+1, web_len+1);
24762469
}
24772470
/* }}} */
24782471

@@ -2485,7 +2478,8 @@ char *phar_create_default_stub(const char *index_php, const char *web_index, siz
24852478
int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int convert, char **error) /* {{{ */
24862479
{
24872480
char halt_stub[] = "__HALT_COMPILER();";
2488-
char *newstub, *tmp;
2481+
zend_string *newstub;
2482+
char *tmp;
24892483
phar_entry_info *entry, *newentry;
24902484
int halt_offset, restore_alias_len, global_flags = 0, closeoldfile;
24912485
char *pos, has_dirs = 0;
@@ -2631,8 +2625,9 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
26312625
newstub = NULL;
26322626
} else {
26332627
/* this is either a brand new phar or a default stub overwrite */
2634-
newstub = phar_create_default_stub(NULL, NULL, &(phar->halt_offset), NULL);
2635-
written = php_stream_write(newfile, newstub, phar->halt_offset);
2628+
newstub = phar_create_default_stub(NULL, NULL, NULL);
2629+
phar->halt_offset = ZSTR_LEN(newstub);
2630+
written = php_stream_write(newfile, ZSTR_VAL(newstub), phar->halt_offset);
26362631
}
26372632
if (phar->halt_offset != written) {
26382633
if (closeoldfile) {
@@ -2647,12 +2642,12 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
26472642
}
26482643
}
26492644
if (newstub) {
2650-
efree(newstub);
2645+
zend_string_free(newstub);
26512646
}
26522647
return EOF;
26532648
}
26542649
if (newstub) {
2655-
efree(newstub);
2650+
zend_string_free(newstub);
26562651
}
26572652
}
26582653
manifest_ftell = php_stream_tell(newfile);

ext/phar/phar_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ int phar_verify_signature(php_stream *fp, size_t end_of_phar, php_uint32 sig_typ
558558
int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, int *signature_length, char **error);
559559

560560
/* utility functions */
561-
char *phar_create_default_stub(const char *index_php, const char *web_index, size_t *len, char **error);
561+
zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error);
562562
char *phar_decompress_filter(phar_entry_info * entry, int return_unknown);
563563
char *phar_compress_filter(phar_entry_info * entry, int return_unknown);
564564

ext/phar/phar_object.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -934,24 +934,22 @@ PHP_METHOD(Phar, interceptFileFuncs)
934934
*/
935935
PHP_METHOD(Phar, createDefaultStub)
936936
{
937-
char *index = NULL, *webindex = NULL, *stub, *error;
937+
char *index = NULL, *webindex = NULL, *error;
938+
zend_string *stub;
938939
size_t index_len = 0, webindex_len = 0;
939-
size_t stub_len;
940940

941941
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ss", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
942942
return;
943943
}
944944

945-
stub = phar_create_default_stub(index, webindex, &stub_len, &error);
945+
stub = phar_create_default_stub(index, webindex, &error);
946946

947947
if (error) {
948948
zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
949949
efree(error);
950950
return;
951951
}
952-
// TODO: avoid reallocation ???
953-
RETVAL_STRINGL(stub, stub_len);
954-
efree(stub);
952+
RETURN_NEW_STR(stub);
955953
}
956954
/* }}} */
957955

@@ -2909,10 +2907,10 @@ PHP_METHOD(Phar, setStub)
29092907
*/
29102908
PHP_METHOD(Phar, setDefaultStub)
29112909
{
2912-
char *index = NULL, *webindex = NULL, *error = NULL, *stub = NULL;
2910+
char *index = NULL, *webindex = NULL, *error = NULL;
2911+
zend_string *stub = NULL;
29132912
size_t index_len = 0, webindex_len = 0;
29142913
int created_stub = 0;
2915-
size_t stub_len = 0;
29162914
PHAR_ARCHIVE_OBJECT();
29172915

29182916
if (phar_obj->archive->is_data) {
@@ -2942,13 +2940,13 @@ PHP_METHOD(Phar, setDefaultStub)
29422940
}
29432941

29442942
if (!phar_obj->archive->is_tar && !phar_obj->archive->is_zip) {
2945-
stub = phar_create_default_stub(index, webindex, &stub_len, &error);
2943+
stub = phar_create_default_stub(index, webindex, &error);
29462944

29472945
if (error) {
29482946
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "%s", error);
29492947
efree(error);
29502948
if (stub) {
2951-
efree(stub);
2949+
zend_string_free(stub);
29522950
}
29532951
RETURN_FALSE;
29542952
}
@@ -2960,10 +2958,10 @@ PHP_METHOD(Phar, setDefaultStub)
29602958
zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
29612959
return;
29622960
}
2963-
phar_flush(phar_obj->archive, stub, stub_len, 1, &error);
2961+
phar_flush(phar_obj->archive, stub ? ZSTR_VAL(stub) : 0, stub ? ZSTR_LEN(stub) : 0, 1, &error);
29642962

29652963
if (created_stub) {
2966-
efree(stub);
2964+
zend_string_free(stub);
29672965
}
29682966

29692967
if (error) {

ext/phar/stub.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
/* $Id$ */
2020

21-
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)
21+
static inline zend_string* phar_get_stub(const char *index_php, const char *web, const int name_len, const int web_len)
2222
{
2323
static const char newstub0[] = "<?php\n\n$web = '";
2424
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
3030

3131
static const int newstub_len = 6665;
3232

33-
*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);
33+
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);
3434
}

0 commit comments

Comments
 (0)