Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ext/zend_test/observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ static ZEND_INI_MH(zend_test_observer_OnUpdateCommaList)
}
if (stage != PHP_INI_STAGE_STARTUP && stage != PHP_INI_STAGE_ACTIVATE && stage != PHP_INI_STAGE_DEACTIVATE && stage != PHP_INI_STAGE_SHUTDOWN) {
ZEND_HASH_FOREACH_STR_KEY(*p, funcname) {
if ((func = zend_hash_find_ptr(EG(function_table), funcname))) {
if ((func = zend_hash_find_ptr(EG(function_table), funcname))
&& RUN_TIME_CACHE(&func->common)) {
void *old_handler;
zend_observer_remove_begin_handler(func, observer_begin, (zend_observer_fcall_begin_handler *)&old_handler);
zend_observer_remove_end_handler(func, observer_end, (zend_observer_fcall_end_handler *)&old_handler);
Expand All @@ -357,7 +358,11 @@ static ZEND_INI_MH(zend_test_observer_OnUpdateCommaList)
zend_string_release(str);
if (stage != PHP_INI_STAGE_STARTUP && stage != PHP_INI_STAGE_ACTIVATE && stage != PHP_INI_STAGE_DEACTIVATE && stage != PHP_INI_STAGE_SHUTDOWN) {
ZEND_HASH_FOREACH_STR_KEY(*p, funcname) {
if ((func = zend_hash_find_ptr(EG(function_table), funcname))) {
if ((func = zend_hash_find_ptr(EG(function_table), funcname))
&& RUN_TIME_CACHE(&func->common) && *ZEND_OBSERVER_DATA(func)) {
void *old_handler;
zend_observer_remove_begin_handler(func, observer_begin, (zend_observer_fcall_begin_handler *)&old_handler);
zend_observer_remove_end_handler(func, observer_end, (zend_observer_fcall_end_handler *)&old_handler);
Comment on lines +363 to +365
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is needed. Any previous handlers should have been removed above, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first loop only removes handlers for functions in the previous comma list. If observe_all=1 installed the handler at first-call time (via observer_fcall_init), that function was never in the old comma list, so the first loop won't touch it. Without the remove here, add_begin_handler installs a duplicate and hits ZEND_UNREACHABLE().

The *ZEND_OBSERVER_DATA(func) check gates this: we only attempt removal when observer data already exists.

zend_observer_add_begin_handler(func, observer_begin);
zend_observer_add_end_handler(func, observer_end);
}
Expand Down
18 changes: 18 additions & 0 deletions ext/zend_test/tests/gh16811.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
GH-16811 (Segmentation fault in zend observer)
--EXTENSIONS--
zend_test
--INI--
zend_test.observer.enabled=1
zend_test.observer.show_output=1
zend_test.observer.observe_function_names=a,d
--FILE--
<?php
var_dump(ini_set("zend_test.observer.observe_function_names", "bar"));
function d() {}
?>
--EXPECTF--
<!-- init '%s' -->
<!-- init ini_set() -->
<!-- init var_dump() -->
string(3) "a,d"
17 changes: 17 additions & 0 deletions ext/zend_test/tests/gh16811_observe_all.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
GH-16811 (Assertion failure adding duplicate observer handler)
--EXTENSIONS--
zend_test
--INI--
zend_test.observer.enabled=1
zend_test.observer.observe_all=1
zend_test.observer.show_output=0
--FILE--
<?php
function foo() {}
foo();
ini_set("zend_test.observer.observe_function_names", "foo");
echo "Done\n";
?>
--EXPECT--
Done
Loading