Skip to content

Commit

Permalink
Make missing trait error recoverable
Browse files Browse the repository at this point in the history
We were already handling NULL as a case, but seem to have forgotten to
pass the ZEND_FETCH_CLASS_EXCEPTION flag.

Fixes phpGH-17959
  • Loading branch information
iluuu1994 committed Mar 3, 2025
1 parent 0097ad8 commit f71c1c4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Zend/tests/traits/bugs/missing-trait.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ $test = new TraitsTest();

?>
--EXPECTF--
Fatal error: Trait "THello" not found in %s on line %d
Fatal error: Uncaught Error: Trait "THello" not found in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
5 changes: 4 additions & 1 deletion Zend/tests/traits/error_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ class A {

?>
--EXPECTF--
Fatal error: Trait "abc" not found in %s on line %d
Fatal error: Uncaught Error: Trait "abc" not found in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
18 changes: 18 additions & 0 deletions Zend/tests/traits/gh17959.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
GH-17959: Missing trait error is recoverable
--FILE--
<?php

try {
class C {
use MissingTrait;
}
} catch (Error $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}

?>
===DONE===
--EXPECT--
Error: Trait "MissingTrait" not found
===DONE===
2 changes: 1 addition & 1 deletion Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -3535,7 +3535,7 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string

for (i = 0; i < ce->num_traits; i++) {
zend_class_entry *trait = zend_fetch_class_by_name(ce->trait_names[i].name,
ce->trait_names[i].lc_name, ZEND_FETCH_CLASS_TRAIT);
ce->trait_names[i].lc_name, ZEND_FETCH_CLASS_TRAIT | ZEND_FETCH_CLASS_EXCEPTION);
if (UNEXPECTED(trait == NULL)) {
free_alloca(traits_and_interfaces, use_heap);
return NULL;
Expand Down

0 comments on commit f71c1c4

Please sign in to comment.