php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70241 Skipped assertions affect Generator returns
Submitted: 2015-08-11 17:58 UTC Modified: 2015-08-11 19:47 UTC
From: me at kelunik dot com Assigned: bwoebi
Status: Closed Package: Scripting Engine problem
PHP Version: 7.0.0beta3 OS:
Private report: No CVE-ID:
 [2015-08-11 17:58 UTC] me at kelunik dot com
Description:
------------
Assertion configuration can change code behavior:

php -d zend.assertions=1 test.php -> Generator
php -d zend.assertions=-1 test.php -> bool(true)

Test script:
---------------
<?php

function foo() {
    assert(yield 42);
    
    return true;
}

var_dump(foo());

Expected result:
----------------
php -d zend.assertions=1 test.php -> Generator
php -d zend.assertions=-1 test.php -> Generator

Actual result:
--------------
php -d zend.assertions=1 test.php -> Generator
php -d zend.assertions=-1 test.php -> bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-11 18:07 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-08-11 18:07 UTC] cmb@php.net
zend.assertions=-1 means don't produce any code, so the script
behaves as if there was no assert statement.
 [2015-08-11 18:16 UTC] bwoebi@php.net
-Status: Not a bug +Status: Re-Opened
 [2015-08-11 18:16 UTC] bwoebi@php.net
Precisely, it means not producing any code. "-1 - don't generate any code (zero-cost, production mode)" from the RFC.

It doesn't mean ignore all the side-effects caused by the content of assert(). It just means ignoring the code generated by it.

So, the reporter is right. Either it's a mistake in the RFC or a bug.
 [2015-08-11 18:20 UTC] bwoebi@php.net
Additional note: zend.assertions=0 and zend.assertions=-1 *should* IMHO behave the same. Which would imply that it definitely should return a Generator in both cases.
 [2015-08-11 18:20 UTC] bwoebi@php.net
Additional note: zend.assertions=0 and zend.assertions=-1 *should* IMHO behave the same. Which would imply that it definitely should return a Generator in both cases.
 [2015-08-11 18:20 UTC] bwoebi@php.net
-Status: Re-Opened +Status: Assigned -Assigned To: cmb +Assigned To: bwoebi
 [2015-08-11 19:39 UTC] cmb@php.net
> It doesn't mean ignore all the side-effects caused by the
> content of assert(). It just means ignoring the code generated
> by it.

Turning off assertions would not make much sense, if only the
overhead of checking the value of the expression would be skipped.
What actually may be very costly is the evaluation the expression
itself (consider checking each element of a large array for some
condition).

AFAIK, "no" programming language does evaluate the expression, if
assertion checking is disabled. At least this is so with PHP 5,
cf. <http://3v4l.org/YLofr> and <http://3v4l.org/RRp6m>.

> Additional note: zend.assertions=0 and zend.assertions=-1
> *should* IMHO behave the same.

ACK. However, IMHO, the current behavior with zend.assertions=0 is
in error.
 [2015-08-11 19:45 UTC] nikic@php.net
To make sure everyone is on the same page: The bug here is not that the `yield` does not get evaluated (that's correct). The bug is that the function is not marked as a generator at all. So zend.assertions=-1 not only skips the assertion, but also influences the behavior of the function as a whole -- which it shouldn't do.
 [2015-08-11 19:47 UTC] bwoebi@php.net
While I agree with you that the current behavior with zend.assertions=0 is not perfect, it's not fixable without major changes in the implementation.
As we need to be able to dynamically toggle assertions off and on.

Hence I'd like to make zend.assertions=0 & assertions=off behave the same than zend.assertions=-1.

All what would need to be fixed is actually walking the AST of the assert expression and checking for ZEND_AST_YIELD. (What I'm going to do.)
 [2015-08-11 20:12 UTC] bwoebi@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=dbd8edbbd08fbb2906ee9b540348b340715b5dbe
Log: Fixed bug #70241 (Skipped assertions affect Generator returns)
 [2015-08-11 20:12 UTC] bwoebi@php.net
-Status: Assigned +Status: Closed
 [2015-08-18 16:24 UTC] ab@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=dbd8edbbd08fbb2906ee9b540348b340715b5dbe
Log: Fixed bug #70241 (Skipped assertions affect Generator returns)
 [2016-07-20 11:37 UTC] davey@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=dbd8edbbd08fbb2906ee9b540348b340715b5dbe
Log: Fixed bug #70241 (Skipped assertions affect Generator returns)
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Feb 28 14:01:40 2017 UTC