Skip to content

Commit 95a17af

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

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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
@@ -2765,6 +2765,19 @@ static void emit_incompatible_trait_constant_error(
27652765
);
27662766
}
27672767

2768+
static void emit_trait_constant_enum_case_conflict_error(
2769+
const zend_class_entry *ce, const zend_class_constant *trait_constant, zend_string *name
2770+
) {
2771+
zend_error_noreturn(E_COMPILE_ERROR,
2772+
"Cannot use trait %s, because %s::%s conflicts with enum case %s::%s",
2773+
ZSTR_VAL(trait_constant->ce->name),
2774+
ZSTR_VAL(trait_constant->ce->name),
2775+
ZSTR_VAL(name),
2776+
ZSTR_VAL(ce->name),
2777+
ZSTR_VAL(name)
2778+
);
2779+
}
2780+
27682781
static bool do_trait_constant_check(
27692782
zend_class_entry *ce, zend_class_constant *trait_constant, zend_string *name, zend_class_entry **traits, size_t current_trait
27702783
) {
@@ -2778,6 +2791,11 @@ static bool do_trait_constant_check(
27782791

27792792
zend_class_constant *existing_constant = Z_PTR_P(zv);
27802793

2794+
if (UNEXPECTED(ZEND_CLASS_CONST_FLAGS(existing_constant) & ZEND_CLASS_CONST_IS_CASE)) {
2795+
emit_trait_constant_enum_case_conflict_error(ce, trait_constant, name);
2796+
return false;
2797+
}
2798+
27812799
if ((ZEND_CLASS_CONST_FLAGS(trait_constant) & flags_mask) != (ZEND_CLASS_CONST_FLAGS(existing_constant) & flags_mask)) {
27822800
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
27832801
return false;

0 commit comments

Comments
 (0)