Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ PHP NEWS
. Fixed a crash when executing a prepared statement again after a failed
execute() with libmysqlclient. (Ilia Alshanetsky)

- PDO_PGSQL:
. Fixed memory leaks when Pdo\Pgsql::setNoticeCallback() callbacks reference
their own nonpersistent connection. (Ilia Alshanetsky, David Carlier)

- PDO_SQLite:
. Fixed bug GH-25588 (Use-after-free of a string parameter omitted on a later
execute() or rebound while fetching). (Ilia Alshanetsky)
Expand Down
4 changes: 4 additions & 0 deletions ext/pdo/pdo_dbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,10 @@ static void pdo_dbh_free_storage(zend_object *std)
return;
}

if (!dbh->is_persistent) {
dbh->is_closed = 1;
}

/* The persistent list holds one reference, other objects may hold the rest */
if (dbh->driver_data && dbh->methods && dbh->methods->rollback
&& (!dbh->is_persistent || dbh->refcount <= 2) && pdo_is_in_transaction(dbh)) {
Expand Down
13 changes: 11 additions & 2 deletions ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static void _pdo_pgsql_notice(void *context, const char *message) /* {{{ */
{
pdo_dbh_t * dbh = (pdo_dbh_t *)context;
zend_fcall_info_cache *fc = ((pdo_pgsql_db_handle *)dbh->driver_data)->notice_callback;
if (fc) {
if (fc && !dbh->is_closed) {
zval zarg;
ZVAL_STRING(&zarg, message);
zend_call_known_fcc(fc, NULL, 1, &zarg, NULL);
Expand Down Expand Up @@ -1329,6 +1329,15 @@ static bool pdo_pgsql_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
}
}

static void pdo_pgsql_get_gc(pdo_dbh_t *dbh, zend_get_gc_buffer *gc_buffer)
{
pdo_pgsql_db_handle *H = dbh->driver_data;

if (!dbh->is_persistent && H && H->notice_callback) {
zend_get_gc_buffer_add_fcc(gc_buffer, H->notice_callback);
}
}

static const struct pdo_dbh_methods pgsql_methods = {
pgsql_handle_closer,
pgsql_handle_preparer,
Expand All @@ -1345,7 +1354,7 @@ static const struct pdo_dbh_methods pgsql_methods = {
pdo_pgsql_get_driver_methods, /* get_driver_methods */
pdo_pgsql_request_shutdown,
pgsql_handle_in_transaction,
NULL, /* get_gc */
pdo_pgsql_get_gc,
pdo_pgsql_scanner
};

Expand Down
55 changes: 55 additions & 0 deletions ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
Pdo\Pgsql notice callback cycles are collected
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';

class NoticeHandler {
public function __construct(public PDO $db) {}

public function handle(string $message): void {
echo trim($message), "\n";
}

public function __invoke(string $message): void {
$this->handle($message);
}
}

foreach (['closure', 'method', 'invokable'] as $kind) {
$db = PDOTest::factory(Pdo\Pgsql::class);
$callback = match ($kind) {
'closure' => function ($message) use ($db) {
echo trim($message), "\n";
},
'method' => [new NoticeHandler($db), 'handle'],
'invokable' => new NoticeHandler($db),
};
$db->setNoticeCallback($callback);
$weak = WeakReference::create($db);
unset($callback);
gc_collect_cycles();
$db->exec("DO $$ BEGIN RAISE NOTICE '$kind'; END $$");
unset($db);
gc_collect_cycles();
echo "$kind collected: ";
var_dump($weak->get() === null);
}
?>
--EXPECT--
NOTICE: closure
closure collected: bool(true)
NOTICE: method
method collected: bool(true)
NOTICE: invokable
invokable collected: bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Persistent Pdo\Pgsql notice callbacks remain alive during garbage collection
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';

putenv('PDOTEST_ATTR=' . serialize([PDO::ATTR_PERSISTENT => true]));
$a = PDOTest::factory(Pdo\Pgsql::class);
$b = PDOTest::factory(Pdo\Pgsql::class);
$f = function ($message) use ($a, $b) {
echo trim($message), "\n";
};
$a->setNoticeCallback($f);
unset($a, $b);
gc_collect_cycles();

$c = PDOTest::factory(Pdo\Pgsql::class);
$c->exec("DO $$ BEGIN RAISE NOTICE 'after gc'; END $$");
var_dump($f instanceof Closure);
$c->setNoticeCallback(null);
?>
--EXPECT--
NOTICE: after gc
bool(true)
47 changes: 47 additions & 0 deletions ext/pdo_pgsql/tests/pdopgsql_setNoticeCallback_gc_teardown.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
Pdo\Pgsql notice callback freed by the garbage collector before its connection
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';
PDOTest::skip();
$db = PDOTest::factory(Pdo\Pgsql::class);
try {
$db->exec("SET log_statement = 'all'");
} catch (PDOException $e) {
die("skip cannot set log_statement");
}
?>
--FILE--
<?php
require __DIR__ . '/config.inc';
require dirname(__DIR__, 2) . '/pdo/tests/pdo_test.inc';

class NoticeHandler {
public string $prefix;

public function __construct(public PDO $db) {
$this->prefix = str_repeat('-', 2);
}

public function __invoke(string $message): void {
echo $this->prefix, " ", trim($message), "\n";
}
}

$db = PDOTest::factory(Pdo\Pgsql::class);
$db->exec("SET log_statement = 'all'");
$db->exec("SET client_min_messages = log");
$handler = new NoticeHandler($db);
$db->setNoticeCallback($handler);
$db->beginTransaction();
gc_collect_cycles();
unset($handler, $db);
var_dump(gc_collect_cycles() > 0);
?>
--EXPECT--
-- LOG: statement: BEGIN
bool(true)
Loading