From 3c2a411df2e9d58c51ca9ba5fabad8fb47353777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 8 May 2025 13:01:09 +0200 Subject: [PATCH] standard: Take `zend.assertions` into account for dynamic calls to `assert()` Fixes php/php-src#18509. --- NEWS | 2 ++ ext/standard/assert.c | 6 +++++- ext/standard/tests/assert/gh18509.phpt | 23 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 ext/standard/tests/assert/gh18509.phpt diff --git a/NEWS b/NEWS index 15b8fa714223b..f5dd8fe320839 100644 --- a/NEWS +++ b/NEWS @@ -34,6 +34,8 @@ PHP NEWS - Standard: . Fixed bug GH-17403 (Potential deadlock when putenv fails). (nielsdos) + . Fixed bug GH-18509 (Dynamic calls to assert() ignore zend.assertions). + (timwolla) - Windows: . Fix leak+crash with sapi_windows_set_ctrl_handler(). (nielsdos) diff --git a/ext/standard/assert.c b/ext/standard/assert.c index f0b6adadbe17d..258447576e1f4 100644 --- a/ext/standard/assert.c +++ b/ext/standard/assert.c @@ -178,7 +178,11 @@ PHP_FUNCTION(assert) zend_string *description_str = NULL; zend_object *description_obj = NULL; - if (!ASSERTG(active)) { + /* EG(assertions) <= 0 is only reachable by dynamic calls to assert(), + * since calls known at compile time will skip the entire call when + * assertions are disabled. + */ + if (!ASSERTG(active) || EG(assertions) <= 0) { RETURN_TRUE; } diff --git a/ext/standard/tests/assert/gh18509.phpt b/ext/standard/tests/assert/gh18509.phpt new file mode 100644 index 0000000000000..2bf1a04c87c69 --- /dev/null +++ b/ext/standard/tests/assert/gh18509.phpt @@ -0,0 +1,23 @@ +--TEST-- +GH-18509: Dynamic calls to assert() ignore zend.assertions +--INI-- +zend.assertions=0 +--FILE-- + +--EXPECT-- +array(3) { + [0]=> + bool(true) + [1]=> + bool(true) + [2]=> + bool(true) +}