Bug #73875 [Com]: assert() evaluated despite runtime assert_options(ASSERT_ACTIVE, 0);

From: Date: Fri, 06 Jan 2017 23:42:05 +0000
Subject: Bug #73875 [Com]: assert() evaluated despite runtime assert_options(ASSERT_ACTIVE, 0);
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206340@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73875&edit=1 ID: 73875 Comment by: vedad at kajtaz dot net Reported by: vedad at kajtaz dot net Summary: assert() evaluated despite runtime assert_options(ASSERT_ACTIVE, 0); Status: Verified Type: Bug Package: Scripting Engine problem Operating System: FreeBSD 10.1 PHP Version: 7.0.14 Block user comment: N Private report: N New Comment: >> Actually, setting zend.assertions to 0 produces the same effect >> - expression in assert is still evaluated. > That would be a bug, but I can't reproduce this, see > <https://3v4l.org/EYrTO>. Please double-check. Indeed, my result seems to have been due to the 'uopz' extension. Disabling uopz produces the correct result. I guess I now need to file a bug for uopz. > Actually, assert.active=0 is not supposed to suppress the > evaluation of the assertion parameter (unless it's a string), but > rather to ignore the result of the evalutation. This works as > expected as long as the assertion expression doesn't have any > side-effects (and usually you don't want side-effects in > assertions, anyway). The point is that both the "assert.active" ini setting documentation, and the assert_options() documentation explicitly state that they "enable" (or, implicitly, disable) evaluation. I believe this makes more sense than the subtle string/non-string discrepancy (though you're right that assertions are not meant to have side effects). Previous Comments: ------------------------------------------------------------------------ [2017-01-06 22:56:22] cmb@php.net > Actually, setting zend.assertions to 0 produces the same effect > - expression in assert is still evaluated. That would be a bug, but I can't reproduce this, see <https://3v4l.org/EYrTO>. Please double-check. Otherwise there may be the need to improve the documentation. Actually, assert.active=0 is not supposed to suppress the evaluation of the assertion parameter (unless it's a string), but rather to ignore the result of the evalutation. This works as expected as long as the assertion expression doesn't have any side-effects (and usually you don't want side-effects in assertions, anyway). > assertion() can now be an expression. Indeed, this changelog info is wrong. An expression was also allowed in former versions, but it was always evaluated, as there has been no zend.assertions (think of the PHP 5 behavior being like zend.assertions=1). ------------------------------------------------------------------------ [2017-01-06 14:32:34] vedad at kajtaz dot net Expressions are allowed in assertions since PHP 7.0: > 7.0.0 assert() is now a language construct and not a function. assertion() can now be an > expression. That being said, it is either undocumented or plain wrong that the expression be evaluated despite asserts being disabled. ------------------------------------------------------------------------ [2017-01-06 14:17:04] fernando at null-life dot com > assert($assert = true); Shouldn't this line be > assert('$assert = true'); After changing it I get the expected result: PHP VERSION 7.0.13 running cli sapi. Initial settings: zend.assertions: 1 assert.active: 1 After altering settings: zend.assertions: 1 assert.active: 0 Code in assert() was NOT evaluated ------------------------------------------------------------------------ [2017-01-06 13:28:54] vedad at kajtaz dot net Actually, setting zend.assertions to 0 produces the same effect - expression in assert is still evaluated. echo 'PHP VERSION '.PHP_VERSION.' running '.PHP_SAPI.' sapi.'.PHP_EOL; echo 'Initial settings:'.PHP_EOL; echo 'zend.assertions: '.ini_get('zend.assertions').PHP_EOL; echo 'assert.active: '.ini_get('assert.active').PHP_EOL; ini_set('zend.assertions', '0'); assert_options(ASSERT_ACTIVE, 0); echo 'After altering settings:'.PHP_EOL; echo 'zend.assertions: '.ini_get('zend.assertions').PHP_EOL; echo 'assert.active: '.ini_get('assert.active').PHP_EOL; $assert = false; assert($assert = true); if($assert) echo 'Code in assert() was evaluated'.PHP_EOL; else echo 'Code in assert() was NOT evaluated'.PHP_EOL; ------------------------------------------------------------------------ [2017-01-05 18:31:11] vedad at kajtaz dot net Description: ------------ Expressions in assert() are evaluated despite setting assert_options(ASSERT_ACTIVE, 0) at runtime. I understand one should use zend.assertions in PHP 7+, yet IMHO this is either a documentation bug, or a misbehavior. Test script: --------------- echo 'PHP VERSION '.PHP_VERSION.' running '.PHP_SAPI.' sapi.'.PHP_EOL; echo 'Initial settings:'.PHP_EOL; echo 'zend.assertions: '.ini_get('zend.assertions').PHP_EOL; echo 'assert.active: '.ini_get('assert.active').PHP_EOL; assert_options(ASSERT_ACTIVE, 0); echo 'After altering settings:'.PHP_EOL; echo 'zend.assertions: '.ini_get('zend.assertions').PHP_EOL; echo 'assert.active: '.ini_get('assert.active').PHP_EOL; $assert = false; assert($assert = true); if($assert) echo 'Code in assert() was evaluated'.PHP_EOL; else echo 'Code in assert() was NOT evaluated'.PHP_EOL; Expected result: ---------------- PHP VERSION 7.0.14 running cli sapi. Initial settings: zend.assertions: 1 assert.active: 1 After altering settings: zend.assertions: 1 assert.active: 0 Code in assert() was NOT evaluated Actual result: -------------- PHP VERSION 7.0.14 running cli sapi. Initial settings: zend.assertions: 1 assert.active: 1 After altering settings: zend.assertions: 1 assert.active: 0 Code in assert() was evaluated ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73875&edit=1

« previous php.bugs (#206340) next »