Skip to content

Commit 2d651e2

Browse files
zend_inheritance: Fix enum case conflict in trait binding (#21771)
Fixes GH-21760.
1 parent f7eb5ef commit 2d651e2

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
@@ -11,6 +11,8 @@ PHP NEWS
1111
. Fixed bug GH-21699 (Assertion failure in shutdown_executor when resolving
1212
self::/parent::/static:: callables if the error handler throws). (macoaure)
1313
. Fixed bug GH-21603 (Missing addref for __unset). (ilutov)
14+
. Fixed bug GH-21760 (Trait with class constant name conflict against
15+
enum case causes SEGV). (Pratik Bhujel)
1416

1517
- CLI:
1618
. 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
@@ -2757,6 +2757,19 @@ static void emit_incompatible_trait_constant_error(
27572757
);
27582758
}
27592759

2760+
static void emit_trait_constant_enum_case_conflict_error(
2761+
const zend_class_entry *ce, const zend_class_constant *trait_constant, zend_string *name
2762+
) {
2763+
zend_error_noreturn(E_COMPILE_ERROR,
2764+
"Cannot use trait %s, because %s::%s conflicts with enum case %s::%s",
2765+
ZSTR_VAL(trait_constant->ce->name),
2766+
ZSTR_VAL(trait_constant->ce->name),
2767+
ZSTR_VAL(name),
2768+
ZSTR_VAL(ce->name),
2769+
ZSTR_VAL(name)
2770+
);
2771+
}
2772+
27602773
static bool do_trait_constant_check(
27612774
zend_class_entry *ce, zend_class_constant *trait_constant, zend_string *name, zend_class_entry **traits, size_t current_trait
27622775
) {
@@ -2770,6 +2783,11 @@ static bool do_trait_constant_check(
27702783

27712784
zend_class_constant *existing_constant = Z_PTR_P(zv);
27722785

2786+
if (UNEXPECTED(ZEND_CLASS_CONST_FLAGS(existing_constant) & ZEND_CLASS_CONST_IS_CASE)) {
2787+
emit_trait_constant_enum_case_conflict_error(ce, trait_constant, name);
2788+
return false;
2789+
}
2790+
27732791
if ((ZEND_CLASS_CONST_FLAGS(trait_constant) & flags_mask) != (ZEND_CLASS_CONST_FLAGS(existing_constant) & flags_mask)) {
27742792
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
27752793
return false;

0 commit comments

Comments
 (0)