Skip to content

Commit 8ba0017

Browse files
author
Stefan Marr
committed
Fixed a inconsitent condition for aliasing traits.
- missed a failing Traits test, but is now fixed, and the bug covered by a dedicated test # Should always comment conditions that go over more than two lines :-/
1 parent db0c957 commit 8ba0017

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

Zend/tests/traits/bugs/alias01.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Aliases are applied to the correct methods, and only to them.
3+
--FILE--
4+
<?php
5+
trait T1 {
6+
function m1() { echo "T:m1\n"; }
7+
function m2() { echo "T:m2\n"; }
8+
}
9+
10+
class C1 {
11+
use T1 { m1 as a1; }
12+
}
13+
14+
$o = new C1;
15+
$o->m1();
16+
$o->a1();
17+
$o->m2();
18+
$o->a2();
19+
20+
?>
21+
--EXPECTF--
22+
T:m1
23+
T:m1
24+
T:m2
25+
26+
Fatal error: Call to undefined method C1::a2() in %s on line %d

Zend/tests/traits/language010.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ $o->world();
2727

2828
?>
2929
--EXPECTF--
30-
Fatal error: Failed to add trait method (world) to the trait table. There is probably already a trait method with the same name in %s on line %d
30+
Fatal error: Trait method world has not been applied, because there are collisions with other trait methods on MyClass in %s on line %d

Zend/zend_compile.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3662,10 +3662,13 @@ static int zend_traits_copy_functions(zend_function *fn TSRMLS_DC, int num_args,
36623662
/* apply aliases which are qualified with a class name, there should not be any ambiguity */
36633663
if (aliases) {
36643664
while (aliases[i]) {
3665-
if (!aliases[i]->trait_method->ce || (fn->common.scope == aliases[i]->trait_method->ce &&
3666-
(zend_binary_strcasecmp(aliases[i]->trait_method->method_name,
3665+
3666+
if (/* Scope unset or equal to the function we compare to */
3667+
(!aliases[i]->trait_method->ce || fn->common.scope == aliases[i]->trait_method->ce)
3668+
&& /* and, the alias applies to fn */
3669+
(zend_binary_strcasecmp(aliases[i]->trait_method->method_name,
36673670
aliases[i]->trait_method->mname_len,
3668-
fn->common.function_name, fnname_len) == 0))) {
3671+
fn->common.function_name, fnname_len) == 0)) {
36693672
if (aliases[i]->alias) {
36703673
fn_copy = *fn;
36713674
zend_traits_duplicate_function(&fn_copy, estrndup(aliases[i]->alias, aliases[i]->alias_len) TSRMLS_CC);
@@ -3703,10 +3706,12 @@ static int zend_traits_copy_functions(zend_function *fn TSRMLS_DC, int num_args,
37033706
if (aliases) {
37043707
i = 0;
37053708
while (aliases[i]) {
3706-
if ((!aliases[i]->trait_method->ce || fn->common.scope == aliases[i]->trait_method->ce) &&
3707-
(zend_binary_strcasecmp(aliases[i]->trait_method->method_name,
3708-
aliases[i]->trait_method->mname_len,
3709-
fn->common.function_name, fnname_len) == 0)) {
3709+
if (/* Scope unset or equal to the function we compare to */
3710+
(!aliases[i]->trait_method->ce || fn->common.scope == aliases[i]->trait_method->ce)
3711+
&& /* and, the alias applies to fn */
3712+
(zend_binary_strcasecmp(aliases[i]->trait_method->method_name,
3713+
aliases[i]->trait_method->mname_len,
3714+
fn->common.function_name, fnname_len) == 0)) {
37103715
if (!aliases[i]->alias && aliases[i]->modifiers) { /* if it is 0, no modifieres has been changed */
37113716
fn_copy.common.fn_flags = aliases[i]->modifiers;
37123717
if (!(aliases[i]->modifiers & ZEND_ACC_PPP_MASK)) {

0 commit comments

Comments
 (0)