php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60978 exit code incorrect
Submitted: 2012-02-04 17:54 UTC Modified: 2012-03-02 03:25 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: thekid@php.net Assigned: laruence
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.0RC7 OS: Windows
Private report: No CVE-ID:
 [2012-02-04 17:54 UTC] thekid@php.net
Description:
------------
Calling exit with an int arg will not lead to PHP exiting with this exitcode.

Test script:
---------------
$ php -r 'exit(2);' ; echo $?

Expected result:
----------------
2

Actual result:
--------------
254

Patches

wrong_exit_code.patch (last revision 2012-02-06 09:55 UTC) by laruence@php.net)
patch_v1 (last revision 2012-02-04 20:40 UTC) by php-dev at zerocue dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-04 17:56 UTC] thekid@php.net
Same goes for die().
 [2012-02-04 19:44 UTC] php-dev at zerocue dot com
Looks like this (on my system) is related to ZEND_VM_SPEC not being defined.

zend_vm_def.h: 4567

#if !defined(ZEND_VM_SPEC) || (OP1_TYPE != IS_UNUSED)

Results in all variants of ZEND_EXIT to become: #if 0 || ...

There are only two references to ZEND_VM_SPEC in the code base, neither are 
defines.  What does this control, where should it be defined?
 [2012-02-04 19:59 UTC] php-dev at zerocue dot com
Actually my last post has nothing to do with it, mis-read the code.  I don't see 
where the problem is, the executor_globals.exit_status is being set to two 
properly and the LONGJMP(bailout) still has the right exit_status value in the 
executor.

I did confirm that this was fine in 5.3.10 but not in 5.4 or trunk.
 [2012-02-04 20:00 UTC] php-dev at zerocue dot com
This does not occur when executing a script file, only through -r.
 [2012-02-04 20:43 UTC] php-dev at zerocue dot com
Alright, in 5.3.10@php_cli:1023 the call to zend_eval_string_ex() never returned 
after a zend_bailout(), if it had this bug would have been present there because 
zend_bailout always returns FAILURE even in non-failure cases.  

In 5.4 and trunk, the zend_eval_string_ex() does return and hits the 
exit_status=254 line.  The proper fix would be to have zend_bailout not return 
FAILURE unless it did fail but this is a much larger change.  In this case the 
patch simply ignores the return state of the zend_eval_string_ex() and always 
returns the EG(exit_status) value.
 [2012-02-05 09:59 UTC] thekid@php.net
Your patch breaks the exitcode 254 for parse errors.

We have the following cases and expected exit codes:

* Clean exit, `php -r 'echo "";' ; echo $?` = 0
* Clean explicit exit with message, `php -r 'exit("test");' ; echo $?` = 0
* Clean explicit exit with exitcode, `php -r 'exit(2);' ; echo $?` = 2
* Exit after a fatal error, `php -r '$fatal->error();' ; echo $?` = 255
* Exit after compile error, `php -r '$->' ; echo $?` = 254
 [2012-02-05 10:01 UTC] thekid@php.net
...oh, and:

* Uncaught exception exit, `php -r 'throw new Exception("test");' ; echo $?` = 255
 [2012-02-05 10:54 UTC] thekid@php.net
Test suite:

<?php
  if (!isset($argv[1])) {
    exit("*** Usage: php exit.php /path/to/php/binary\n");
  }
  $php= realpath($argv[1]);
  if (!is_executable($php)) {
    exit("*** PHP Binary '$argv[1]' is not executable\n");
  }
  
  $tests= array(
    "echo '';"                     => 0,
    "exit('test');"                => 0,
    "exit(2);"                     => 2,
    "fatal();"                     => 255,
    "\$->"                         => 254,
    "throw new Exception('test');" => 255
  );
  
  foreach ($tests as $source => $expected) {
    $cmd= '"'.$php.'" -r "'.$source.'" 2>&1';
    $output= array();
    exec($cmd, $output, $actual);
    if ($actual === $expected) {
      printf("%-30s: [OK]\n", $source);
    } else {
      printf("%-30s: [FAIL, expect %d, have %d (%s)]\n", $source, $expected, $actual, implode(' ', $output));
    }
  }
?>
 [2012-02-05 13:36 UTC] thekid@php.net
-Assigned To: +Assigned To: derick
 [2012-02-05 13:36 UTC] thekid@php.net
Checking SVN history revealed r322922 as the cause - see http://svn.php.net/viewvc?view=revision&revision=322922. Reverting it fixes this bug.

The commit mentions a relation to bug #60218 but I'm not sure why it "Reinstated correct return values". Before this fix the code in zend_execute_API.c read:

    zend_execute(new_op_array TSRMLS_CC);
    [...]
    retval = SUCCESS;

After the fix it read:

    zend_try {
        zend_execute(new_op_array TSRMLS_CC);
    } zend_end_try();
    [...]
    retval = SUCCESS;

Finally, r322922 changed it to:

    retval = SUCCESS;
    zend_try {
        zend_execute(new_op_array TSRMLS_CC);
    } zend_catch {
        retval = FAILURE;
    } zend_end_try();

The zend_catch  "block" is executed whenever SETJMP returns non-zero, so basically when LONGJMP is called, which is the case for zend_bailout(), which again is used by exit() and die(). To my eyes, this *changed* the return value instead of reinstating it. 


@derick, maybe you can shed some light on this commit?
 [2012-02-06 09:55 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: wrong_exit_code.patch
Revision:   1328522147
URL:        https://bugs.php.net/patch-display.php?bug=60978&patch=wrong_exit_code.patch&revision=1328522147
 [2012-02-06 09:57 UTC] laruence@php.net
I think a appropriate way to fix these issues(memleak, xdebug, this issue) is 
catch->efree->throw.  I have made a patch. 

Derick, what do you think?
 [2012-02-08 03:03 UTC] laruence@php.net
Automatic comment from SVN on behalf of laruence
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=323120
Log: Revert -r319102 and -r322922 in 5.4 branch since they introduce #60978
Fixed #60978 in trunk without reverting previous fix
#see http://news.php.net/php.internals/57789
 [2012-02-08 03:10 UTC] laruence@php.net
-Assigned To: derick +Assigned To: laruence
 [2012-02-08 03:10 UTC] laruence@php.net
fixed in trunk, and also "make it works" in 5.4 ,  I will close this after I ci 
the final fix to 5.4(as long as 5.4.0 release)
 [2012-03-02 03:25 UTC] laruence@php.net
Automatic comment from SVN on behalf of laruence
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=323771
Log: MFH: Fixed bug #60978 (exit code incorrect)
 [2012-03-02 03:25 UTC] laruence@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2012-03-02 03:25 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 [2012-04-18 09:45 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=101e3e8aed6920c2e39833e7b3f98943556fc93e
Log: MFH: Fixed bug #60978 (exit code incorrect)
 [2012-04-18 09:46 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ff63c09e6f4e5c7119aaf00d2af9c55f68cff168
Log: Revert -r319102 and -r322922 in 5.4 branch since they introduce #60978 Fixed #60978 in trunk without reverting previous fix #see http://news.php.net/php.internals/57789
 [2012-07-24 23:37 UTC] rasmus@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=101e3e8aed6920c2e39833e7b3f98943556fc93e
Log: MFH: Fixed bug #60978 (exit code incorrect)
 [2012-07-24 23:37 UTC] rasmus@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ff63c09e6f4e5c7119aaf00d2af9c55f68cff168
Log: Revert -r319102 and -r322922 in 5.4 branch since they introduce #60978 Fixed #60978 in trunk without reverting previous fix #see http://news.php.net/php.internals/57789
 [2013-11-17 09:33 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=101e3e8aed6920c2e39833e7b3f98943556fc93e
Log: MFH: Fixed bug #60978 (exit code incorrect)
 [2013-11-17 09:33 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ff63c09e6f4e5c7119aaf00d2af9c55f68cff168
Log: Revert -r319102 and -r322922 in 5.4 branch since they introduce #60978 Fixed #60978 in trunk without reverting previous fix #see http://news.php.net/php.internals/57789
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 08:02:55 2014 UTC