diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2020-03-02 11:07:57 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-03-03 11:55:48 +0100 |
commit | 53efa1b0c69b463ea9d3606d828c036129f2dec9 (patch) | |
tree | c4b2a0e33acce36638f200af3b1add704a2e2e88 /Zend/zend_inheritance.c | |
parent | a7a2e9857e96abc2147e895f8d844a6e73e86170 (diff) | |
download | php-git-53efa1b0c69b463ea9d3606d828c036129f2dec9.tar.gz |
Store aliased name of trait method
Currently, trait methods are aliased will continue to use the
original function name. In a few places in the codebase, we will
try to look up the actual method name instead. However, this does
not work if an aliased method is used indirectly
(https://bugs.php.net/bug.php?id=69180).
I think it would be better to instead actually change the method
name to the alias. This is in principle easy: We have to allow
function_name to be changed even if op array is otherwise shared
(similar to static_variables). This means we need to addref/release
the function_name separately, but I don't think there is a
performance concern here (especially as everything is usually
interned).
There is a bit of complication in opcache, where we need to make
sure that the function name is released the correct number of times
(interning may overwrite the name in the original op_array, but we
need to release it as many times as the op_array is shared).
Fixes bug #69180.
Fixes bug #74939.
Closes GH-5226.
Diffstat (limited to 'Zend/zend_inheritance.c')
-rw-r--r-- | Zend/zend_inheritance.c | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c index c875427a70..eb79aab098 100644 --- a/Zend/zend_inheritance.c +++ b/Zend/zend_inheritance.c @@ -115,6 +115,9 @@ static zend_always_inline zend_function *zend_duplicate_function(zend_function * if (func->op_array.refcount) { (*func->op_array.refcount)++; } + if (EXPECTED(func->op_array.function_name)) { + zend_string_addref(func->op_array.function_name); + } if (is_interface || EXPECTED(!func->op_array.static_variables)) { /* reuse the same op_array structure */ @@ -1577,7 +1580,7 @@ static void zend_add_magic_methods(zend_class_entry* ce, zend_string* mname, zen } /* }}} */ -static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_string *key, zend_function *fn, HashTable **overridden) /* {{{ */ +static void zend_add_trait_method(zend_class_entry *ce, zend_string *name, zend_string *key, zend_function *fn, HashTable **overridden) /* {{{ */ { zend_function *existing_fn = NULL; zend_function *new_fn; @@ -1622,11 +1625,11 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s /* two traits can't define the same non-abstract method */ #if 1 zend_error_noreturn(E_COMPILE_ERROR, "Trait method %s has not been applied, because there are collisions with other trait methods on %s", - name, ZSTR_VAL(ce->name)); + ZSTR_VAL(name), ZSTR_VAL(ce->name)); #else /* TODO: better error message */ zend_error_noreturn(E_COMPILE_ERROR, "Trait method %s::%s has not been applied as %s::%s, because of collision with %s::%s", ZSTR_VAL(fn->common.scope->name), ZSTR_VAL(fn->common.function_name), - ZSTR_VAL(ce->name), name, + ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(existing_fn->common.scope->name), ZSTR_VAL(existing_fn->common.function_name)); #endif } else { @@ -1647,6 +1650,9 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s new_fn->op_array.fn_flags |= ZEND_ACC_TRAIT_CLONE; new_fn->op_array.fn_flags &= ~ZEND_ACC_IMMUTABLE; } + + /* Reassign method name, in case it is an alias. */ + new_fn->common.function_name = name; function_add_ref(new_fn); fn = zend_hash_update_ptr(&ce->function_table, key, new_fn); zend_add_magic_methods(ce, key, fn); @@ -1695,7 +1701,7 @@ static void zend_traits_copy_functions(zend_string *fnname, zend_function *fn, z } lcname = zend_string_tolower(alias->alias); - zend_add_trait_method(ce, ZSTR_VAL(alias->alias), lcname, &fn_copy, overridden); + zend_add_trait_method(ce, alias->alias, lcname, &fn_copy, overridden); zend_string_release_ex(lcname, 0); /* Record the trait from which this alias was resolved. */ @@ -1747,7 +1753,7 @@ static void zend_traits_copy_functions(zend_string *fnname, zend_function *fn, z } } - zend_add_trait_method(ce, ZSTR_VAL(fn->common.function_name), fnname, &fn_copy, overridden); + zend_add_trait_method(ce, fn->common.function_name, fnname, &fn_copy, overridden); } } /* }}} */ |