Skip to content

Commit ca27e40

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Added gc_handler to properly handle circular references. (#16703)
2 parents 9a255b3 + 4d14325 commit ca27e40

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.3.15
44

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

68
07 Nov 2024, PHP 8.3.14RC1
79

ext/pdo/pdo_stmt.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,23 @@ static zend_function *dbstmt_method_get(zend_object **object_pp, zend_string *me
20762076
return fbc;
20772077
}
20782078

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

@@ -2492,6 +2509,7 @@ void pdo_stmt_init(void)
24922509
pdo_dbstmt_object_handlers.get_method = dbstmt_method_get;
24932510
pdo_dbstmt_object_handlers.compare = zend_objects_not_comparable;
24942511
pdo_dbstmt_object_handlers.clone_obj = NULL;
2512+
pdo_dbstmt_object_handlers.get_gc = dbstmt_get_gc;
24952513

24962514
pdo_row_ce = register_class_PDORow();
24972515
pdo_row_ce->create_object = pdo_row_new;

ext/pdo/tests/gh16703.phpt

Lines changed: 48 additions & 0 deletions
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)