Skip to content

Commit 4d14325

Browse files
SakiTakamachicmb69
andcommitted
Added gc_handler to properly handle circular references. (#16703)
closes #16703 Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
1 parent 3f1ea1b commit 4d14325

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.2.27
44

5+
- PDO:
6+
. Fixed memory leak of `setFetchMode()`. (SakiTakamachi)
57

68
21 Nov 2024, PHP 8.2.26
79

ext/pdo/pdo_stmt.c

+18
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,23 @@ static zend_function *dbstmt_method_get(zend_object **object_pp, zend_string *me
20772077
return fbc;
20782078
}
20792079

2080+
static HashTable *dbstmt_get_gc(zend_object *object, zval **gc_data, int *gc_count)
2081+
{
2082+
pdo_stmt_t *stmt = php_pdo_stmt_fetch_object(object);
2083+
*gc_data = &stmt->fetch.into;
2084+
*gc_count = 1;
2085+
2086+
/**
2087+
* If there are no dynamic properties and the default property is 1 (that is, there is only one property
2088+
* of string that does not participate in GC), there is no need to call zend_std_get_properties().
2089+
*/
2090+
if (object->properties == NULL && object->ce->default_properties_count <= 1) {
2091+
return NULL;
2092+
} else {
2093+
return zend_std_get_properties(object);
2094+
}
2095+
}
2096+
20802097
zend_object_handlers pdo_dbstmt_object_handlers;
20812098
zend_object_handlers pdo_row_object_handlers;
20822099

@@ -2495,6 +2512,7 @@ void pdo_stmt_init(void)
24952512
pdo_dbstmt_object_handlers.get_method = dbstmt_method_get;
24962513
pdo_dbstmt_object_handlers.compare = zend_objects_not_comparable;
24972514
pdo_dbstmt_object_handlers.clone_obj = NULL;
2515+
pdo_dbstmt_object_handlers.get_gc = dbstmt_get_gc;
24982516

24992517
pdo_row_ce = register_class_PDORow();
25002518
pdo_row_ce->create_object = pdo_row_new;

ext/pdo/tests/gh16703.phpt

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
GH-16703: Memory leak of setFetchMode()
3+
--EXTENSIONS--
4+
pdo
5+
--SKIPIF--
6+
<?php
7+
$dir = getenv('REDIR_TEST_DIR');
8+
if (false == $dir) die('skip no driver');
9+
require_once $dir . 'pdo_test.inc';
10+
PDOTest::skip();
11+
?>
12+
--FILE--
13+
<?php
14+
if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/');
15+
class TestStmt extends PDOStatement
16+
{
17+
public $name;
18+
}
19+
20+
$db = new PDO(
21+
getenv('PDOTEST_DSN'),
22+
getenv('PDOTEST_USER') ?: null,
23+
getenv('PDOTEST_PASS') ?: null,
24+
[
25+
PDO::ATTR_CASE => PDO::CASE_LOWER,
26+
PDO::ATTR_STATEMENT_CLASS => [TestStmt::class],
27+
],
28+
);
29+
30+
$db->exec('CREATE TABLE gh16703 (name varchar(255))');
31+
$db->exec("INSERT INTO gh16703 (name) VALUES ('test_name')");
32+
33+
$stmt = $db->query('SELECT name FROM gh16703');
34+
$t = $stmt;
35+
$stmt->setFetchMode(PDO::FETCH_INTO, $stmt);
36+
$stmt->fetch();
37+
echo "done!\n";
38+
?>
39+
--CLEAN--
40+
<?php
41+
if (getenv('PDOTEST_DSN') === 'sqlite::memory:') return;
42+
43+
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
44+
$db = PDOTest::factory();
45+
$db->exec('DROP TABLE gh16703');
46+
?>
47+
--EXPECT--
48+
done!

0 commit comments

Comments
 (0)