Skip to content

Commit 72cf32f

Browse files
committed
Added gc_handler to properly handle circular references.
1 parent 5ddb756 commit 72cf32f

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

ext/pdo/pdo_stmt.c

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
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+
[PDO::ATTR_STATEMENT_CLASS => [TestStmt::class]],
25+
);
26+
27+
$db->exec('CREATE TABLE gh16703 (name varchar(255))');
28+
$db->exec("INSERT INTO gh16703 (name) VALUES ('test_name')");
29+
30+
$stmt = $db->query('SELECT name FROM gh16703');
31+
$t = $stmt;
32+
$stmt->setFetchMode(PDO::FETCH_INTO, $stmt);
33+
$stmt->fetch();
34+
echo "done!\n";
35+
?>
36+
--CLEAN--
37+
<?php
38+
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
39+
$db = PDOTest::factory();
40+
$db->exec('DROP TABLE gh16703');
41+
?>
42+
--EXPECT--
43+
done!

0 commit comments

Comments
 (0)