Skip to content

Commit 896dad4

Browse files
committed
Fixed bug #77561
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.
1 parent 74c4381 commit 896dad4

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ PHP NEWS
1313
. Fixed bug #79897 (Promoted constructor params with attribs cause crash).
1414
(Deus Kane)
1515
. Fixed bug #79946 (Build fails due to undeclared UINT32_C). (Nikita)
16+
. Fixed bug #77561 (Shebang line not stripped for non-primary script).
17+
(Nikita)
1618

1719
- Date:
1820
. Fixed bug #60302 (DateTime::createFromFormat should new static(), not new

Zend/zend_language_scanner.l

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle)
576576
}
577577

578578
if (CG(skip_shebang)) {
579-
CG(skip_shebang) = 0;
580579
BEGIN(SHEBANG);
581580
} else {
582581
BEGIN(INITIAL);

main/main.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,20 +2533,7 @@ PHPAPI int php_execute_script(zend_file_handle *primary_file)
25332533
zend_set_timeout(INI_INT("max_execution_time"), 0);
25342534
}
25352535

2536-
/*
2537-
If cli primary file has shebang line and there is a prepend file,
2538-
the `skip_shebang` will be used by prepend file but not primary file,
2539-
save it and restore after prepend file been executed.
2540-
*/
2541-
if (CG(skip_shebang) && prepend_file_p) {
2542-
CG(skip_shebang) = 0;
2543-
if (zend_execute_scripts(ZEND_REQUIRE, NULL, 1, prepend_file_p) == SUCCESS) {
2544-
CG(skip_shebang) = 1;
2545-
retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 2, primary_file, append_file_p) == SUCCESS);
2546-
}
2547-
} else {
2548-
retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS);
2549-
}
2536+
retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS);
25502537
} zend_end_try();
25512538

25522539
if (EG(exception)) {

sapi/cli/tests/bug77561.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env php
2+
<?php
3+
declare(strict_types=1);
4+
echo "Test\n";

sapi/cli/tests/bug77561.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Bug #77561: Shebang line not stripped for non-primary script
3+
--FILE--
4+
<?php
5+
6+
require __DIR__ . '/bug77561.inc';
7+
8+
?>
9+
--EXPECT--
10+
Test

0 commit comments

Comments
 (0)