Skip to content

Commit f2f45f3

Browse files
authored
phar: Use object_init_with_constructor where possible (#20156)
* phar: Use object_init_with_constructor where possible We could also chain the exceptions, but the current code keeps the existing behaviour by adding EG(exception) checks. * Drop redundant exception throwing
1 parent 869c458 commit f2f45f3

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

ext/phar/phar_object.c

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ PHP_METHOD(Phar, buildFromDirectory)
17111711
{
17121712
char *error;
17131713
bool apply_reg = false;
1714-
zval arg, arg2, iter, iteriter, regexiter;
1714+
zval iter, iteriter, regexiter;
17151715
struct _phar_t pass;
17161716
zend_string *dir, *regex = NULL;
17171717

@@ -1727,34 +1727,16 @@ PHP_METHOD(Phar, buildFromDirectory)
17271727
RETURN_THROWS();
17281728
}
17291729

1730-
if (SUCCESS != object_init_ex(&iter, spl_ce_RecursiveDirectoryIterator)) {
1731-
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate directory iterator for %s", phar_obj->archive->fname);
1732-
RETURN_THROWS();
1733-
}
1734-
1735-
ZVAL_STR(&arg, dir);
1736-
ZVAL_LONG(&arg2, SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS);
1730+
zval args[2];
1731+
ZVAL_STR(&args[0], dir);
1732+
ZVAL_LONG(&args[1], SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS);
17371733

1738-
zend_call_known_instance_method_with_2_params(spl_ce_RecursiveDirectoryIterator->constructor,
1739-
Z_OBJ(iter), NULL, &arg, &arg2);
1740-
1741-
if (EG(exception)) {
1742-
zval_ptr_dtor(&iter);
1734+
if (SUCCESS != object_init_with_constructor(&iter, spl_ce_RecursiveDirectoryIterator, 2, args, NULL)) {
17431735
RETURN_THROWS();
17441736
}
17451737

1746-
if (SUCCESS != object_init_ex(&iteriter, spl_ce_RecursiveIteratorIterator)) {
1738+
if (SUCCESS != object_init_with_constructor(&iteriter, spl_ce_RecursiveIteratorIterator, 1, &iter, NULL)) {
17471739
zval_ptr_dtor(&iter);
1748-
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate directory iterator for %s", phar_obj->archive->fname);
1749-
RETURN_THROWS();
1750-
}
1751-
1752-
zend_call_known_instance_method_with_1_params(spl_ce_RecursiveIteratorIterator->constructor,
1753-
Z_OBJ(iteriter), NULL, &iter);
1754-
1755-
if (EG(exception)) {
1756-
zval_ptr_dtor(&iter);
1757-
zval_ptr_dtor(&iteriter);
17581740
RETURN_THROWS();
17591741
}
17601742

@@ -1763,15 +1745,13 @@ PHP_METHOD(Phar, buildFromDirectory)
17631745
if (regex && ZSTR_LEN(regex) > 0) {
17641746
apply_reg = true;
17651747

1766-
if (SUCCESS != object_init_ex(&regexiter, spl_ce_RegexIterator)) {
1748+
ZVAL_COPY_VALUE(&args[0], &iteriter);
1749+
ZVAL_STR(&args[1], regex);
1750+
1751+
if (SUCCESS != object_init_with_constructor(&regexiter, spl_ce_RegexIterator, 2, args, NULL)) {
17671752
zval_ptr_dtor(&iteriter);
1768-
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate regex iterator for %s", phar_obj->archive->fname);
17691753
RETURN_THROWS();
17701754
}
1771-
1772-
ZVAL_STR(&arg2, regex);
1773-
zend_call_known_instance_method_with_2_params(spl_ce_RegexIterator->constructor,
1774-
Z_OBJ(regexiter), NULL, &iteriter, &arg2);
17751755
}
17761756

17771757
array_init(return_value);
@@ -2199,15 +2179,12 @@ static zend_object *phar_rename_archive(phar_archive_data **sphar, char *ext) /*
21992179
ce = phar_ce_archive;
22002180
}
22012181

2202-
if (SUCCESS != object_init_ex(&ret, ce)) {
2203-
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate phar object when converting archive \"%s\"", phar->fname);
2182+
ZVAL_STRINGL(&arg1, phar->fname, phar->fname_len);
2183+
zend_result result = object_init_with_constructor(&ret, ce, 1, &arg1, NULL);
2184+
zval_ptr_dtor_str(&arg1);
2185+
if (SUCCESS != result) {
22042186
return NULL;
22052187
}
2206-
2207-
ZVAL_STRINGL(&arg1, phar->fname, phar->fname_len);
2208-
2209-
zend_call_known_instance_method_with_1_params(ce->constructor, Z_OBJ(ret), NULL, &arg1);
2210-
zval_ptr_dtor(&arg1);
22112188
return Z_OBJ(ret);
22122189

22132190
err_reused_oldpath:

0 commit comments

Comments
 (0)