Skip to content

Commit 336d64c

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

File tree

2 files changed

+65
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)