Skip to content

Fix exception in assert() callback with bail enabled #16304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions Zend/tests/gh16293_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-16293: Exception in assert() callback with bail enabled
--FILE--
<?php

assert_options(ASSERT_EXCEPTION, 0);
assert_options(ASSERT_BAIL, 1);
assert_options(ASSERT_CALLBACK, 'f1');
assert(false);

?>
--EXPECTF--
Warning: assert(): assert(false) failed in %s on line %d

Warning: Uncaught Error: Invalid callback f1, function "f1" not found or invalid function name in %s:%d
Stack trace:
#0 %s(%d): assert(false, 'assert(false)')
#1 {main}
thrown in %s on line %d
19 changes: 19 additions & 0 deletions Zend/tests/gh16293_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-16293: Exception in assert() callback with bail enabled
--FILE--
<?php

assert_options(ASSERT_EXCEPTION, 0);
assert_options(ASSERT_BAIL, 1);
assert_options(ASSERT_CALLBACK, function () {
throw new Exception('Boo');
});
assert(false);

?>
--EXPECTF--
Warning: assert(): assert(false) failed in %s on line %d

Warning: Uncaught Exception: Boo in %s:%d
Stack trace:
%a
5 changes: 5 additions & 0 deletions ext/standard/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ PHP_FUNCTION(assert)
}

if (ASSERTG(bail)) {
if (EG(exception)) {
/* The callback might have thrown. Use E_WARNING to print the
* exception so we can avoid bailout and use unwind_exit. */
zend_exception_error(EG(exception), E_WARNING);
}
zend_throw_unwind_exit();
RETURN_THROWS();
} else {
Expand Down
Loading