Skip to content

Commit

Permalink
Fixed bug #77561
Browse files Browse the repository at this point in the history
Unconditionally strip shebang lines when using the CLI SAPI,
independently of whether they occur in the primary or non-primary
script. It's unlikely that someone intentionally wants to print
that shebang line when including a script, and this regularly
causes issues when scripts are used in multiple contexts, e.g.
for direct invocation and as a phar bootstrap.
  • Loading branch information
nikic committed Aug 10, 2020
1 parent 74c4381 commit 896dad4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ PHP NEWS
. Fixed bug #79897 (Promoted constructor params with attribs cause crash).
(Deus Kane)
. Fixed bug #79946 (Build fails due to undeclared UINT32_C). (Nikita)
. Fixed bug #77561 (Shebang line not stripped for non-primary script).
(Nikita)

- Date:
. Fixed bug #60302 (DateTime::createFromFormat should new static(), not new
Expand Down
1 change: 0 additions & 1 deletion Zend/zend_language_scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle)
}

if (CG(skip_shebang)) {
CG(skip_shebang) = 0;
BEGIN(SHEBANG);
} else {
BEGIN(INITIAL);
Expand Down
15 changes: 1 addition & 14 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2533,20 +2533,7 @@ PHPAPI int php_execute_script(zend_file_handle *primary_file)
zend_set_timeout(INI_INT("max_execution_time"), 0);
}

/*
If cli primary file has shebang line and there is a prepend file,
the `skip_shebang` will be used by prepend file but not primary file,
save it and restore after prepend file been executed.
*/
if (CG(skip_shebang) && prepend_file_p) {
CG(skip_shebang) = 0;
if (zend_execute_scripts(ZEND_REQUIRE, NULL, 1, prepend_file_p) == SUCCESS) {
CG(skip_shebang) = 1;
retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 2, primary_file, append_file_p) == SUCCESS);
}
} else {
retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS);
}
retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS);
} zend_end_try();

if (EG(exception)) {
Expand Down
4 changes: 4 additions & 0 deletions sapi/cli/tests/bug77561.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env php
<?php
declare(strict_types=1);
echo "Test\n";
10 changes: 10 additions & 0 deletions sapi/cli/tests/bug77561.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Bug #77561: Shebang line not stripped for non-primary script
--FILE--
<?php

require __DIR__ . '/bug77561.inc';

?>
--EXPECT--
Test

0 comments on commit 896dad4

Please sign in to comment.