From 962346543e6bb37016269feebd60a5ce0ead4801 Mon Sep 17 00:00:00 2001
From: "Christoph M. Becker" <cmb@php.net>
Date: Wed, 20 Mar 2019 15:04:50 +0800
Subject: [PATCH 1/2] Close file handels after including and requiring files.
Fixes bug #76801
---
sapi/phpdbg/phpdbg_list.c | 3 +++
sapi/phpdbg/tests/bug76801-1.phpt | 16 ++++++++++++++++
2 files changed, 19 insertions(+)
create mode 100644 sapi/phpdbg/tests/bug76801-1.phpt
diff --git a/sapi/phpdbg/phpdbg_list.c b/sapi/phpdbg/phpdbg_list.c
index ad5cf5de09dc..19dd265265e0 100644
--- a/sapi/phpdbg/phpdbg_list.c
+++ b/sapi/phpdbg/phpdbg_list.c
@@ -288,6 +288,7 @@ zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) {
phpdbg_resolve_pending_file_break(ZSTR_VAL(ret->filename));
fake.opened_path = NULL;
+ zend_destroy_file_handle(&fake);
zend_file_handle_dtor(&fake);
return ret;
@@ -315,6 +316,8 @@ zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
}
op_array = PHPDBG_G(init_compile_file)(file, type);
+ zend_destroy_file_handle(file);
+ zend_file_handle_dtor(file);
if (op_array == NULL) {
return NULL;
diff --git a/sapi/phpdbg/tests/bug76801-1.phpt b/sapi/phpdbg/tests/bug76801-1.phpt
new file mode 100644
index 000000000000..cd3d2ddc72c4
--- /dev/null
+++ b/sapi/phpdbg/tests/bug76801-1.phpt
@@ -0,0 +1,16 @@
+--TEST--
+get_included_files() tests
+--FILE--
+<?php
+
+file_put_contents(__DIR__ . '/bug76801-2.php', '<?php echo __LINE__;');
+require(__DIR__."/bug76801-2.php");
+file_put_contents(__DIR__ . '/bug76801-2.php', '<?php');
+
+echo "Done\n";
+?>
+--EXPECTF--
+1Done
+--CLEAN--
+<?php
+@unlink(__DIR__.'/bug76801-2.php');
From c6922deec925daac90b2f8f474c698b55fccdd7a Mon Sep 17 00:00:00 2001
From: Andrew Nicols <andrew@nicols.co.uk>
Date: Wed, 20 Mar 2019 15:05:27 +0800
Subject: [PATCH 2/2] Ensure that included files are added to included_files
This change also adds the original file included to mimic the behaviour
seen in PHP.
---
sapi/phpdbg/phpdbg_list.c | 4 +++
sapi/phpdbg/tests/bug76801-2.inc | 3 +++
sapi/phpdbg/tests/bug76801-2.phpt | 44 +++++++++++++++++++++++++++++++
sapi/phpdbg/tests/clean_001.phpt | 2 +-
sapi/phpdbg/tests/info_001.phpt | 3 ++-
5 files changed, 54 insertions(+), 2 deletions(-)
create mode 100644 sapi/phpdbg/tests/bug76801-2.inc
create mode 100644 sapi/phpdbg/tests/bug76801-2.phpt
diff --git a/sapi/phpdbg/phpdbg_list.c b/sapi/phpdbg/phpdbg_list.c
index 19dd265265e0..cd5a4c61895d 100644
--- a/sapi/phpdbg/phpdbg_list.c
+++ b/sapi/phpdbg/phpdbg_list.c
@@ -287,6 +287,9 @@ zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) {
zend_hash_add_ptr(&PHPDBG_G(file_sources), ret->filename, dataptr);
phpdbg_resolve_pending_file_break(ZSTR_VAL(ret->filename));
+ if (file->opened_path) {
+ zend_hash_add_empty_element(&EG(included_files), file->opened_path);
+ }
fake.opened_path = NULL;
zend_destroy_file_handle(&fake);
zend_file_handle_dtor(&fake);
@@ -306,6 +309,7 @@ zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
if (file->opened_path) {
zend_string_release(file->opened_path);
file->opened_path = zend_string_init(filename, strlen(filename), 0);
+ zend_hash_add_empty_element(&EG(included_files), file->opened_path);
} else {
if (file->free_filename) {
efree((char *) file->filename);
diff --git a/sapi/phpdbg/tests/bug76801-2.inc b/sapi/phpdbg/tests/bug76801-2.inc
new file mode 100644
index 000000000000..58637365860e
--- /dev/null
+++ b/sapi/phpdbg/tests/bug76801-2.inc
@@ -0,0 +1,3 @@
+<?php
+echo "Included\n";
+?>
diff --git a/sapi/phpdbg/tests/bug76801-2.phpt b/sapi/phpdbg/tests/bug76801-2.phpt
new file mode 100644
index 000000000000..a5db266be43a
--- /dev/null
+++ b/sapi/phpdbg/tests/bug76801-2.phpt
@@ -0,0 +1,44 @@
+--TEST--
+get_included_files() tests
+--FILE--
+<?php
+
+var_dump(get_included_files());
+
+include(__DIR__."/bug76801-2.inc");
+var_dump(get_included_files());
+
+include_once(__DIR__."/bug76801-2.inc");
+var_dump(get_included_files());
+
+include(__DIR__."/bug76801-2.inc");
+var_dump(get_included_files());
+
+echo "Done\n";
+?>
+--EXPECTF--
+array(1) {
+ [0]=>
+ string(%d) "%s"
+}
+Included
+array(2) {
+ [0]=>
+ string(%d) "%s"
+ [1]=>
+ string(%d) "%s"
+}
+array(2) {
+ [0]=>
+ string(%d) "%s"
+ [1]=>
+ string(%d) "%s"
+}
+Included
+array(2) {
+ [0]=>
+ string(%d) "%s"
+ [1]=>
+ string(%d) "%s"
+}
+Done
diff --git a/sapi/phpdbg/tests/clean_001.phpt b/sapi/phpdbg/tests/clean_001.phpt
index 050085044877..d8cd5ce21a6d 100644
--- a/sapi/phpdbg/tests/clean_001.phpt
+++ b/sapi/phpdbg/tests/clean_001.phpt
@@ -32,7 +32,7 @@ prompt> Do you really want to clean your current environment? (type y or n): Cle
Classes %d
Functions %d
Constants %d
-Includes 0
+Includes 1
prompt> [Not running]
prompt> 1
[Breakpoint #0 at %s:4, hits: 1]
diff --git a/sapi/phpdbg/tests/info_001.phpt b/sapi/phpdbg/tests/info_001.phpt
index 2f38fd36af6b..f3fc6b171f08 100644
--- a/sapi/phpdbg/tests/info_001.phpt
+++ b/sapi/phpdbg/tests/info_001.phpt
@@ -48,7 +48,8 @@ prompt> ------------------------------------------------
Function Breakpoints:
#0 foo
prompt> [User-defined constants (0)]
-prompt> [Included files: 0]
+prompt> [Included files: 1]
+File: %s/info_001.php
prompt> [No error found!]
prompt> [Literal Constants in foo() (2)]
|-------- C0 -------> [var_dump]