Skip to content

Commit 95d98bf

Browse files
committed
Use zend_hash_find_ptr_lc() for case-insensitive name lookups (batch 2)
Continues #22565 for four sites that were missed: _class_exists_impl(), function_exists(), pdo_stmt.c's dbstmt_method_get(), ReflectionClass:: hasMethod()/getMethod(), and phpdbg's print func/method commands. No behavior change.
1 parent 5b79dc5 commit 95d98bf

4 files changed

Lines changed: 17 additions & 42 deletions

File tree

Zend/zend_builtin_functions.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,6 @@ flf_clean:;
10701070

10711071
static zend_always_inline void _class_exists_impl(zval *return_value, zend_string *name, bool autoload, int flags, int skip_flags) /* {{{ */
10721072
{
1073-
zend_string *lcname;
10741073
const zend_class_entry *ce;
10751074

10761075
if (ZSTR_HAS_CE_CACHE(name)) {
@@ -1083,14 +1082,10 @@ static zend_always_inline void _class_exists_impl(zval *return_value, zend_strin
10831082
if (!autoload) {
10841083
if (ZSTR_VAL(name)[0] == '\\') {
10851084
/* Ignore leading "\" */
1086-
lcname = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
1087-
zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
1085+
ce = zend_hash_str_find_ptr_lc(EG(class_table), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
10881086
} else {
1089-
lcname = zend_string_tolower(name);
1087+
ce = zend_hash_find_ptr_lc(EG(class_table), name);
10901088
}
1091-
1092-
ce = zend_hash_find_ptr(EG(class_table), lcname);
1093-
zend_string_release_ex(lcname, 0);
10941089
} else {
10951090
ce = zend_lookup_class(name);
10961091
}
@@ -1172,23 +1167,18 @@ ZEND_FUNCTION(function_exists)
11721167
{
11731168
zend_string *name;
11741169
bool exists;
1175-
zend_string *lcname;
11761170

11771171
ZEND_PARSE_PARAMETERS_START(1, 1)
11781172
Z_PARAM_STR(name)
11791173
ZEND_PARSE_PARAMETERS_END();
11801174

11811175
if (ZSTR_VAL(name)[0] == '\\') {
11821176
/* Ignore leading "\" */
1183-
lcname = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
1184-
zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
1177+
exists = zend_hash_str_find_ptr_lc(EG(function_table), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1) != NULL;
11851178
} else {
1186-
lcname = zend_string_tolower(name);
1179+
exists = zend_hash_find_ptr_lc(EG(function_table), name) != NULL;
11871180
}
11881181

1189-
exists = zend_hash_exists(EG(function_table), lcname);
1190-
zend_string_release_ex(lcname, 0);
1191-
11921182
RETURN_BOOL(exists);
11931183
}
11941184
/* }}} */

ext/pdo/pdo_stmt.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,12 +1943,9 @@ static void dbstmt_prop_delete(zend_object *object, zend_string *name, void **ca
19431943
static zend_function *dbstmt_method_get(zend_object **object_pp, zend_string *method_name, const zval *key)
19441944
{
19451945
zend_function *fbc = NULL;
1946-
zend_string *lc_method_name;
19471946
zend_object *object = *object_pp;
19481947

1949-
lc_method_name = zend_string_tolower(method_name);
1950-
1951-
if ((fbc = zend_hash_find_ptr(&object->ce->function_table, lc_method_name)) == NULL) {
1948+
if ((fbc = zend_hash_find_ptr_lc(&object->ce->function_table, method_name)) == NULL) {
19521949
pdo_stmt_t *stmt = php_pdo_stmt_fetch_object(object);
19531950
/* instance not created by PDO object */
19541951
if (!stmt->dbh) {
@@ -1964,14 +1961,13 @@ static zend_function *dbstmt_method_get(zend_object **object_pp, zend_string *me
19641961
}
19651962
}
19661963

1967-
if ((fbc = zend_hash_find_ptr(stmt->dbh->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_STMT], lc_method_name)) == NULL) {
1964+
if ((fbc = zend_hash_find_ptr_lc(stmt->dbh->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_STMT], method_name)) == NULL) {
19681965
goto out;
19691966
}
19701967
/* got it */
19711968
}
19721969

19731970
out:
1974-
zend_string_release_ex(lc_method_name, 0);
19751971
if (!fbc) {
19761972
fbc = zend_std_get_method(object_pp, method_name, key);
19771973
}

ext/reflection/php_reflection.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4408,9 +4408,8 @@ ZEND_METHOD(ReflectionClass, hasMethod)
44084408
}
44094409

44104410
GET_REFLECTION_OBJECT_PTR(ce);
4411-
zend_string *lc_name = zend_string_tolower(name);
4412-
RETVAL_BOOL(zend_hash_exists(&ce->function_table, lc_name) || is_closure_invoke(ce, lc_name));
4413-
zend_string_release(lc_name);
4411+
RETVAL_BOOL(zend_hash_find_ptr_lc(&ce->function_table, name) != NULL
4412+
|| (ce == zend_ce_closure && zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))));
44144413
}
44154414
/* }}} */
44164415

@@ -4419,35 +4418,35 @@ ZEND_METHOD(ReflectionClass, getMethod)
44194418
{
44204419
reflection_object *intern;
44214420
zend_class_entry *ce;
4421+
zend_function *mptr;
4422+
zval obj_tmp;
44224423
zend_string *name;
4424+
bool is_invoke;
44234425

44244426
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
44254427
RETURN_THROWS();
44264428
}
44274429

44284430
GET_REFLECTION_OBJECT_PTR(ce);
4429-
zend_string *lc_name = zend_string_tolower(name);
4430-
zend_function *mptr;
4431-
zval obj_tmp;
4432-
if (!Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
4431+
is_invoke = ce == zend_ce_closure && zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE));
4432+
if (!Z_ISUNDEF(intern->obj) && is_invoke
44334433
&& (mptr = zend_get_closure_invoke_method(Z_OBJ(intern->obj))) != NULL)
44344434
{
44354435
/* don't assign closure_object since we only reflect the invoke handler
44364436
method and not the closure definition itself */
44374437
reflection_method_factory(ce, mptr, NULL, return_value);
4438-
} else if (Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
4438+
} else if (Z_ISUNDEF(intern->obj) && is_invoke
44394439
&& object_init_ex(&obj_tmp, ce) == SUCCESS && (mptr = zend_get_closure_invoke_method(Z_OBJ(obj_tmp))) != NULL) {
44404440
/* don't assign closure_object since we only reflect the invoke handler
44414441
method and not the closure definition itself */
44424442
reflection_method_factory(ce, mptr, NULL, return_value);
44434443
zval_ptr_dtor(&obj_tmp);
4444-
} else if ((mptr = zend_hash_find_ptr(&ce->function_table, lc_name)) != NULL) {
4444+
} else if ((mptr = zend_hash_find_ptr_lc(&ce->function_table, name)) != NULL) {
44454445
reflection_method_factory(ce, mptr, NULL, return_value);
44464446
} else {
44474447
zend_throw_exception_ex(reflection_exception_ptr, 0,
44484448
"Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
44494449
}
4450-
zend_string_release(lc_name);
44514450
}
44524451
/* }}} */
44534452

sapi/phpdbg/phpdbg_print.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,8 @@ PHPDBG_PRINT(method) /* {{{ */
153153

154154
if (phpdbg_safe_class_lookup(param->method.class, strlen(param->method.class), &ce) == SUCCESS) {
155155
zend_function *fbc;
156-
zend_string *lcname = zend_string_alloc(strlen(param->method.name), 0);
157-
zend_str_tolower_copy(ZSTR_VAL(lcname), param->method.name, ZSTR_LEN(lcname));
158156

159-
if ((fbc = zend_hash_find_ptr(&ce->function_table, lcname))) {
157+
if ((fbc = zend_hash_str_find_ptr_lc(&ce->function_table, param->method.name, strlen(param->method.name)))) {
160158
phpdbg_notice("%s Method %s (%d ops)",
161159
(fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
162160
ZSTR_VAL(fbc->common.function_name),
@@ -166,8 +164,6 @@ PHPDBG_PRINT(method) /* {{{ */
166164
} else {
167165
phpdbg_error("The method %s::%s could not be found", param->method.class, param->method.name);
168166
}
169-
170-
zend_string_release(lcname);
171167
} else {
172168
phpdbg_error("The class %s could not be found", param->method.class);
173169
}
@@ -181,7 +177,6 @@ PHPDBG_PRINT(func) /* {{{ */
181177
zend_function* fbc;
182178
const char *func_name = param->str;
183179
size_t func_name_len = param->len;
184-
zend_string *lcname;
185180
/* search active scope if begins with period */
186181
if (func_name[0] == '.') {
187182
zend_class_entry *scope = zend_get_executed_scope();
@@ -202,11 +197,8 @@ PHPDBG_PRINT(func) /* {{{ */
202197
func_table = EG(function_table);
203198
}
204199

205-
lcname = zend_string_alloc(func_name_len, 0);
206-
zend_str_tolower_copy(ZSTR_VAL(lcname), func_name, ZSTR_LEN(lcname));
207-
208200
phpdbg_try_access {
209-
if ((fbc = zend_hash_find_ptr(func_table, lcname))) {
201+
if ((fbc = zend_hash_str_find_ptr_lc(func_table, func_name, func_name_len))) {
210202
phpdbg_notice("%s %s %s (%d ops)",
211203
(fbc->type == ZEND_USER_FUNCTION) ? "User" : "Internal",
212204
(fbc->common.scope) ? "Method" : "Function",
@@ -221,8 +213,6 @@ PHPDBG_PRINT(func) /* {{{ */
221213
phpdbg_error("Couldn't fetch function %.*s, invalid data source", (int) func_name_len, func_name);
222214
} phpdbg_end_try_access();
223215

224-
efree(lcname);
225-
226216
return SUCCESS;
227217
} /* }}} */
228218

0 commit comments

Comments
 (0)