Skip to content

Commit b5c09b1

Browse files
committed
Fix exception in assert() callback with bail enabled
Fixes GH-16293 Closes GH-16304
1 parent b817a4f commit b5c09b1

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ PHP NEWS
5757
- SPL:
5858
. Fixed bug GH-16337 (Use-after-free in SplHeap). (nielsdos)
5959

60+
- Standard:
61+
. Fixed bug GH-16293 (Failed assertion when throwing in assert() callback with
62+
bail enabled). (ilutov)
63+
6064
- XMLReader:
6165
. Fixed bug GH-16292 (Segmentation fault in ext/xmlreader/php_xmlreader.c).
6266
(nielsdos)

Zend/tests/gh16293_001.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-16293: Exception in assert() callback with bail enabled
3+
--FILE--
4+
<?php
5+
6+
assert_options(ASSERT_EXCEPTION, 0);
7+
assert_options(ASSERT_BAIL, 1);
8+
assert_options(ASSERT_CALLBACK, 'f1');
9+
assert(false);
10+
11+
?>
12+
--EXPECTF--
13+
Warning: assert(): assert(false) failed in %s on line %d
14+
15+
Warning: Uncaught Error: Invalid callback f1, function "f1" not found or invalid function name in %s:%d
16+
Stack trace:
17+
#0 %s(%d): assert(false, 'assert(false)')
18+
#1 {main}
19+
thrown in %s on line %d

Zend/tests/gh16293_002.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-16293: Exception in assert() callback with bail enabled
3+
--FILE--
4+
<?php
5+
6+
assert_options(ASSERT_EXCEPTION, 0);
7+
assert_options(ASSERT_BAIL, 1);
8+
assert_options(ASSERT_CALLBACK, function () {
9+
throw new Exception('Boo');
10+
});
11+
assert(false);
12+
13+
?>
14+
--EXPECTF--
15+
Warning: assert(): assert(false) failed in %s on line %d
16+
17+
Warning: Uncaught Exception: Boo in %s:%d
18+
Stack trace:
19+
%a

ext/standard/assert.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ PHP_FUNCTION(assert)
195195
}
196196

197197
if (ASSERTG(bail)) {
198+
if (EG(exception)) {
199+
/* The callback might have thrown. Use E_WARNING to print the
200+
* exception so we can avoid bailout and use unwind_exit. */
201+
zend_exception_error(EG(exception), E_WARNING);
202+
}
198203
zend_throw_unwind_exit();
199204
RETURN_THROWS();
200205
} else {

0 commit comments

Comments
 (0)