php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch 0001-Fixed-bug-61828-Memleak-when-calling-Directory-Recur for SPL related Bug #61828

Patch version 2012-04-30 03:47 UTC

Return to Bug #61828 | Download this patch
Patch Revisions:

Developer: reeze.xia@gmail.com

From 34378269494bae2677f4c6c1f3afccb4291b8ac4 Mon Sep 17 00:00:00 2001
From: reeze <reeze.xia@gmail.com>
Date: Mon, 30 Apr 2012 11:37:22 +0800
Subject: [PATCH] Fixed bug #61828 Memleak when calling
 Directory(Recursive)Iterator/Spl(Temp)FileObject ctor twice

---
 ext/spl/spl_directory.c     |   22 ++++++++++++++++++++++
 ext/spl/tests/bug61828.phpt |   26 ++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 ext/spl/tests/bug61828.phpt

diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c
index 04da4e6..3786a6e 100755
--- a/ext/spl/spl_directory.c
+++ b/ext/spl/spl_directory.c
@@ -697,6 +697,12 @@ void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, long ctor_fla
 
 	intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
 	intern->flags = flags;
+
+	if (intern->_path) {
+		efree(intern->_path);
+		php_stream_close(intern->u.dir.dirp);
+	}
+
 #ifdef HAVE_GLOB
 	if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_GLOB) && strstr(path, "glob://") != path) {
 		spprintf(&path, 0, "glob://%s", path);
@@ -2274,6 +2280,14 @@ SPL_METHOD(SplFileObject, __construct)
 
 	zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling TSRMLS_CC);
 
+	if (intern->file_name) {
+		php_stream_close(intern->u.dir.dirp);
+		efree(intern->_path);
+		efree(intern->file_name);
+		efree(intern->orig_path);
+		efree(intern->u.file.open_mode);
+	}
+
 	intern->u.file.open_mode = NULL;
 	intern->u.file.open_mode_len = 0;
 
@@ -2338,6 +2352,14 @@ SPL_METHOD(SplTempFileObject, __construct)
 		return;
 	}
 
+	if (intern->file_name) {
+		php_stream_close(intern->u.dir.dirp);
+		efree(intern->_path);
+		efree(intern->file_name);
+		efree(intern->orig_path);
+		efree(intern->u.file.open_mode);
+	}
+
 	if (max_memory < 0) {
 		intern->file_name = "php://memory";
 		intern->file_name_len = 12;
diff --git a/ext/spl/tests/bug61828.phpt b/ext/spl/tests/bug61828.phpt
new file mode 100644
index 0000000..4078924
--- /dev/null
+++ b/ext/spl/tests/bug61828.phpt
@@ -0,0 +1,26 @@
+--TEST--
+Testing Directory(Recursive)Iterator/Spl(Temp)FileObject ctor twice
+--FILE--
+<?php
+$x = new DirectoryIterator(".");
+$x->__construct("..");
+
+$x = new RecursiveDirectoryIterator(".");
+$x->__construct("..");
+
+$x = new FilesystemIterator(".");
+$x->__construct("..");
+
+$x = new GlobIterator(".");
+$x->__construct("..");
+
+$x = new SplFileObject('.');
+$x->__construct('..');
+
+$x = new SplTempFileObject(1);
+$x->__construct(1);
+
+echo "done!\n";
+?>
+--EXPECT--
+done!
\ No newline at end of file
-- 
1.7.9.3

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 12:01:27 2024 UTC