Skip to content

Commit eec5f87

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 0dc8a54 commit eec5f87

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
@@ -4385,9 +4385,8 @@ ZEND_METHOD(ReflectionClass, hasMethod)
43854385
}
43864386

43874387
GET_REFLECTION_OBJECT_PTR(ce);
4388-
zend_string *lc_name = zend_string_tolower(name);
4389-
RETVAL_BOOL(zend_hash_exists(&ce->function_table, lc_name) || is_closure_invoke(ce, lc_name));
4390-
zend_string_release(lc_name);
4388+
RETVAL_BOOL(zend_hash_find_ptr_lc(&ce->function_table, name) != NULL
4389+
|| (ce == zend_ce_closure && zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE))));
43914390
}
43924391
/* }}} */
43934392

@@ -4396,35 +4395,35 @@ ZEND_METHOD(ReflectionClass, getMethod)
43964395
{
43974396
const reflection_object *intern;
43984397
zend_class_entry *ce;
4398+
zend_function *mptr;
4399+
zval obj_tmp;
43994400
zend_string *name;
4401+
bool is_invoke;
44004402

44014403
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
44024404
RETURN_THROWS();
44034405
}
44044406

44054407
GET_REFLECTION_OBJECT_PTR(ce);
4406-
zend_string *lc_name = zend_string_tolower(name);
4407-
zend_function *mptr;
4408-
zval obj_tmp;
4409-
if (!Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
4408+
is_invoke = ce == zend_ce_closure && zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE));
4409+
if (!Z_ISUNDEF(intern->obj) && is_invoke
44104410
&& (mptr = zend_get_closure_invoke_method(Z_OBJ(intern->obj))) != NULL)
44114411
{
44124412
/* don't assign closure_object since we only reflect the invoke handler
44134413
method and not the closure definition itself */
44144414
reflection_method_factory(ce, mptr, NULL, return_value);
4415-
} else if (Z_ISUNDEF(intern->obj) && is_closure_invoke(ce, lc_name)
4415+
} else if (Z_ISUNDEF(intern->obj) && is_invoke
44164416
&& object_init_ex(&obj_tmp, ce) == SUCCESS && (mptr = zend_get_closure_invoke_method(Z_OBJ(obj_tmp))) != NULL) {
44174417
/* don't assign closure_object since we only reflect the invoke handler
44184418
method and not the closure definition itself */
44194419
reflection_method_factory(ce, mptr, NULL, return_value);
44204420
zval_ptr_dtor(&obj_tmp);
4421-
} else if ((mptr = zend_hash_find_ptr(&ce->function_table, lc_name)) != NULL) {
4421+
} else if ((mptr = zend_hash_find_ptr_lc(&ce->function_table, name)) != NULL) {
44224422
reflection_method_factory(ce, mptr, NULL, return_value);
44234423
} else {
44244424
zend_throw_exception_ex(reflection_exception_ptr, 0,
44254425
"Method %s::%s() does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
44264426
}
4427-
zend_string_release(lc_name);
44284427
}
44294428
/* }}} */
44304429

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)