Skip to content

ext/soap: Refactor userland function calling code #19055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions ext/soap/soap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,9 @@ PHP_METHOD(SoapServer, __construct)
service->version = version;
service->type = SOAP_FUNCTIONS;
service->soap_functions.functions_all = false;
service->soap_functions.ft = zend_new_array(0);
ALLOC_HASHTABLE(service->soap_functions.ft);
/* This hashtable contains zend_function pointers so doesn't need a destructor */
zend_hash_init(service->soap_functions.ft, 0, NULL, NULL, false);

if (wsdl) {
service->sdl = get_sdl(ZEND_THIS, ZSTR_VAL(wsdl), cache_wsdl);
Expand Down Expand Up @@ -1123,7 +1125,7 @@ PHP_METHOD(SoapServer, setObject)
PHP_METHOD(SoapServer, getFunctions)
{
soapServicePtr service;
HashTable *ft = NULL;
const HashTable *ft = NULL;

if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
Expand All @@ -1139,11 +1141,7 @@ PHP_METHOD(SoapServer, getFunctions)
} else if (service->soap_functions.functions_all) {
ft = EG(function_table);
} else if (service->soap_functions.ft != NULL) {
zval *name;

ZEND_HASH_MAP_FOREACH_VAL(service->soap_functions.ft, name) {
add_next_index_str(return_value, zend_string_copy(Z_STR_P(name)));
} ZEND_HASH_FOREACH_END();
ft = service->soap_functions.ft;
}
if (ft != NULL) {
zend_function *f;
Expand All @@ -1162,7 +1160,7 @@ PHP_METHOD(SoapServer, getFunctions)
PHP_METHOD(SoapServer, addFunction)
{
soapServicePtr service;
zval *function_name, function_copy;
zval *function_name;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &function_name) == FAILURE) {
RETURN_THROWS();
Expand All @@ -1178,7 +1176,9 @@ PHP_METHOD(SoapServer, addFunction)

if (service->soap_functions.ft == NULL) {
service->soap_functions.functions_all = false;
service->soap_functions.ft = zend_new_array(zend_hash_num_elements(Z_ARRVAL_P(function_name)));
ALLOC_HASHTABLE(service->soap_functions.ft);
/* This hashtable contains zend_function pointers so doesn't need a destructor */
zend_hash_init(service->soap_functions.ft, zend_hash_num_elements(Z_ARRVAL_P(function_name)), NULL, NULL, false);
}

ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(function_name), tmp_function) {
Expand All @@ -1191,15 +1191,15 @@ PHP_METHOD(SoapServer, addFunction)
}

key = zend_string_tolower(Z_STR_P(tmp_function));
f = zend_hash_find_ptr(EG(function_table), key);

if ((f = zend_hash_find_ptr(EG(function_table), key)) == NULL) {
if (f == NULL) {
zend_string_release_ex(key, false);
zend_type_error("SoapServer::addFunction(): Function \"%s\" not found", Z_STRVAL_P(tmp_function));
RETURN_THROWS();
}

ZVAL_STR_COPY(&function_copy, f->common.function_name);
zend_hash_update(service->soap_functions.ft, key, &function_copy);
zend_hash_update_ptr(service->soap_functions.ft, key, f);

zend_string_release_ex(key, 0);
} ZEND_HASH_FOREACH_END();
Expand All @@ -1209,19 +1209,20 @@ PHP_METHOD(SoapServer, addFunction)
zend_function *f;

key = zend_string_tolower(Z_STR_P(function_name));

if ((f = zend_hash_find_ptr(EG(function_table), key)) == NULL) {
f = zend_hash_find_ptr(EG(function_table), key);
if (f == NULL) {
zend_string_release_ex(key, false);
zend_argument_type_error(1, "must be a valid function name, function \"%s\" not found", Z_STRVAL_P(function_name));
RETURN_THROWS();
}
if (service->soap_functions.ft == NULL) {
service->soap_functions.functions_all = false;
service->soap_functions.ft = zend_new_array(0);
ALLOC_HASHTABLE(service->soap_functions.ft);
/* This hashtable contains zend_function pointers so doesn't need a destructor */
zend_hash_init(service->soap_functions.ft, 0, NULL, NULL, false);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet is repeated 2 times, and the allocation with the comment 3 times. I wonder if this can be split off to a helper function (e.g. "soap_ensure_functions_table" or something alike). Could be left for a follow-up


ZVAL_STR_COPY(&function_copy, f->common.function_name);
zend_hash_update(service->soap_functions.ft, key, &function_copy);
zend_hash_update_ptr(service->soap_functions.ft, key, f);
zend_string_release_ex(key, 0);
} else if (Z_TYPE_P(function_name) == IS_LONG) {
if (Z_LVAL_P(function_name) == SOAP_FUNCTIONS_ALL) {
Expand Down