Skip to content

Commit 1c74bab

Browse files
committed
More efficient check for valid class name
Use a bitset of valid characters instead of strspn. This is both more efficient and more compact.
1 parent bda099a commit 1c74bab

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Zend/zend_execute_API.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,28 @@ ZEND_API void zend_call_known_instance_method_with_2_params(
910910
zend_call_known_instance_method(fn, object, retval_ptr, 2, params);
911911
}
912912

913+
/* 0-9 a-z A-Z _ \ 0x80-0xff */
914+
static const uint32_t valid_chars[8] = {
915+
0x00000000,
916+
0x03ff0000,
917+
0x97fffffe,
918+
0x07fffffe,
919+
0xffffffff,
920+
0xffffffff,
921+
0xffffffff,
922+
0xffffffff,
923+
};
924+
925+
static zend_bool zend_is_valid_class_name(zend_string *name) {
926+
for (size_t i = 0; i < ZSTR_LEN(name); i++) {
927+
unsigned char c = ZSTR_VAL(name)[i];
928+
if (!ZEND_BIT_TEST(valid_chars, c)) {
929+
return 0;
930+
}
931+
}
932+
return 1;
933+
}
934+
913935
ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *key, uint32_t flags) /* {{{ */
914936
{
915937
zend_class_entry *ce = NULL;
@@ -966,7 +988,7 @@ ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *
966988
}
967989

968990
/* Verify class name before passing it to the autoloader. */
969-
if (!key && strspn(ZSTR_VAL(name), "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377\\") != ZSTR_LEN(name)) {
991+
if (!key && !zend_is_valid_class_name(name)) {
970992
zend_string_release_ex(lc_name, 0);
971993
return NULL;
972994
}

0 commit comments

Comments
 (0)