Skip to content

Commit 2de529f

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: zend_inheritance: Fix enum case conflict in trait binding (#21771)
2 parents ec79850 + 2d651e2 commit 2de529f

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ PHP NEWS
1414
. Fixed bug GH-21699 (Assertion failure in shutdown_executor when resolving
1515
self::/parent::/static:: callables if the error handler throws). (macoaure)
1616
. Fixed bug GH-21603 (Missing addref for __unset). (ilutov)
17+
. Fixed bug GH-21760 (Trait with class constant name conflict against
18+
enum case causes SEGV). (Pratik Bhujel)
1719

1820
- CLI:
1921
. Fixed bug GH-21754 (`--rf` command line option with a method triggers

Zend/tests/enum/gh21760.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-21760 (Trait with class constant name conflict against enum case causes SEGV)
3+
--FILE--
4+
<?php
5+
6+
trait X {
7+
public const Up = 1;
8+
}
9+
10+
enum Direction {
11+
use X;
12+
13+
case Up;
14+
case Down;
15+
}
16+
17+
?>
18+
--EXPECTF--
19+
Fatal error: Cannot use trait X, because X::Up conflicts with enum case Direction::Up in %s on line %d

Zend/zend_inheritance.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,19 @@ static void emit_incompatible_trait_constant_error(
27732773
);
27742774
}
27752775

2776+
static void emit_trait_constant_enum_case_conflict_error(
2777+
const zend_class_entry *ce, const zend_class_constant *trait_constant, zend_string *name
2778+
) {
2779+
zend_error_noreturn(E_COMPILE_ERROR,
2780+
"Cannot use trait %s, because %s::%s conflicts with enum case %s::%s",
2781+
ZSTR_VAL(trait_constant->ce->name),
2782+
ZSTR_VAL(trait_constant->ce->name),
2783+
ZSTR_VAL(name),
2784+
ZSTR_VAL(ce->name),
2785+
ZSTR_VAL(name)
2786+
);
2787+
}
2788+
27762789
static bool do_trait_constant_check(
27772790
zend_class_entry *ce, zend_class_constant *trait_constant, zend_string *name, zend_class_entry **traits, size_t current_trait
27782791
) {
@@ -2786,6 +2799,11 @@ static bool do_trait_constant_check(
27862799

27872800
zend_class_constant *existing_constant = Z_PTR_P(zv);
27882801

2802+
if (UNEXPECTED(ZEND_CLASS_CONST_FLAGS(existing_constant) & ZEND_CLASS_CONST_IS_CASE)) {
2803+
emit_trait_constant_enum_case_conflict_error(ce, trait_constant, name);
2804+
return false;
2805+
}
2806+
27892807
if ((ZEND_CLASS_CONST_FLAGS(trait_constant) & flags_mask) != (ZEND_CLASS_CONST_FLAGS(existing_constant) & flags_mask)) {
27902808
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
27912809
return false;

0 commit comments

Comments
 (0)