Skip to content

Commit 3828b24

Browse files
committed
ext/phar: Refactor phar_create_default_stub()
1 parent 0b37777 commit 3828b24

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

ext/phar/phar.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,42 +2453,48 @@ static int phar_flush_clean_deleted_apply(zval *zv) /* {{{ */
24532453

24542454
#include "stub.h" /* Generated phar_get_stub() function from makestub.php script */
24552455

2456-
zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error) /* {{{ */
2456+
zend_string *phar_create_default_stub(const zend_string *php_index_str, const zend_string *web_index_str, char **error) /* {{{ */
24572457
{
2458-
size_t index_len, web_len;
2458+
const char *php_index;
2459+
const char *web_index;
2460+
size_t php_len, web_len;
24592461

24602462
if (error) {
24612463
*error = NULL;
24622464
}
24632465

2464-
if (!index_php) {
2465-
index_php = "index.php";
2466-
}
2467-
2468-
if (!web_index) {
2469-
web_index = "index.php";
2470-
}
2471-
2472-
index_len = strlen(index_php);
2473-
web_len = strlen(web_index);
2474-
2475-
if (index_len > 400) {
2476-
/* ridiculous size not allowed for index.php startup filename */
2477-
if (error) {
2478-
spprintf(error, 0, "Illegal filename passed in for stub creation, was %zd characters long, and only 400 or less is allowed", index_len);
2466+
if (!php_index_str) {
2467+
php_index = "index.php";
2468+
php_len = strlen("index.php");
2469+
} else {
2470+
php_index = ZSTR_VAL(php_index_str);
2471+
php_len = ZSTR_LEN(php_index_str);
2472+
if (php_len > 400) {
2473+
/* ridiculous size not allowed for index.php startup filename */
2474+
if (error) {
2475+
spprintf(error, 0, "Illegal filename passed in for stub creation, was %zd characters long, and only 400 or less is allowed", php_len);
2476+
}
24792477
return NULL;
24802478
}
24812479
}
24822480

2483-
if (web_len > 400) {
2484-
/* ridiculous size not allowed for index.php startup filename */
2485-
if (error) {
2486-
spprintf(error, 0, "Illegal web filename passed in for stub creation, was %zd characters long, and only 400 or less is allowed", web_len);
2481+
if (!web_index_str) {
2482+
web_index = "index.php";
2483+
web_len = strlen("index.php");
2484+
} else {
2485+
web_index = ZSTR_VAL(web_index_str);
2486+
web_len = ZSTR_LEN(web_index_str);
2487+
2488+
if (web_len > 400) {
2489+
/* ridiculous size not allowed for index.php startup filename */
2490+
if (error) {
2491+
spprintf(error, 0, "Illegal web filename passed in for stub creation, was %zd characters long, and only 400 or less is allowed", web_len);
2492+
}
24872493
return NULL;
24882494
}
24892495
}
24902496

2491-
return phar_get_stub(index_php, web_index, index_len+1, web_len+1);
2497+
return phar_get_stub(php_index, web_index, php_len+1, web_len+1);
24922498
}
24932499
/* }}} */
24942500

ext/phar/phar_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s
415415
ZEND_ATTRIBUTE_NONNULL zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, size_t *signature_length, char **error);
416416

417417
/* utility functions */
418-
zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error);
418+
zend_string *phar_create_default_stub(const zend_string *php_index_str, const zend_string *web_index_str, char **error);
419419
const char *phar_decompress_filter(const phar_entry_info *entry, bool return_unknown);
420420
const char *phar_compress_filter(const phar_entry_info *entry, bool return_unknown);
421421

ext/phar/phar_object.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -946,11 +946,11 @@ PHP_METHOD(Phar, interceptFileFuncs)
946946
*/
947947
PHP_METHOD(Phar, createDefaultStub)
948948
{
949-
char *index = NULL, *webindex = NULL, *error;
949+
zend_string *index = NULL, *webindex = NULL;
950+
char *error;
950951
zend_string *stub;
951-
size_t index_len = 0, webindex_len = 0;
952952

953-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|p!p!", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
953+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|P!P!", &index, &webindex) == FAILURE) {
954954
RETURN_THROWS();
955955
}
956956

@@ -2914,12 +2914,11 @@ PHP_METHOD(Phar, setStub)
29142914
*/
29152915
PHP_METHOD(Phar, setDefaultStub)
29162916
{
2917-
char *index = NULL, *webindex = NULL, *error = NULL;
2917+
zend_string *index = NULL, *webindex = NULL;
29182918
zend_string *stub = NULL;
2919-
size_t index_len = 0, webindex_len = 0;
29202919
bool created_stub = false;
29212920

2922-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
2921+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|P!P!", &index, &webindex) == FAILURE) {
29232922
RETURN_THROWS();
29242923
}
29252924

@@ -2947,6 +2946,7 @@ PHP_METHOD(Phar, setDefaultStub)
29472946
RETURN_THROWS();
29482947
}
29492948

2949+
char *error = NULL;
29502950
if (!phar_obj->archive->is_tar && !phar_obj->archive->is_zip) {
29512951
stub = phar_create_default_stub(index, webindex, &error);
29522952

0 commit comments

Comments
 (0)