From c02aa461268843270a6897c20ef3fb4b00d9de8a Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 16 Nov 2021 16:14:29 +0100 Subject: [PATCH] Fix leak when persistent PDO connection fails As we don't register the resource, the resource dtor is not called and will not decrement the refcount. --- ext/pdo/pdo_dbh.c | 4 ++++ .../tests/construct_persistent_failure.phpt | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 ext/pdo_mysql/tests/construct_persistent_failure.phpt diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index d53d2ca63c0..5d3efe12e55 100644 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -419,6 +419,10 @@ options: } /* the connection failed; things will tidy up in free_storage */ + if (is_persistent) { + dbh->refcount--; + } + /* XXX raise exception */ zend_restore_error_handling(&zeh); if (!EG(exception)) { diff --git a/ext/pdo_mysql/tests/construct_persistent_failure.phpt b/ext/pdo_mysql/tests/construct_persistent_failure.phpt new file mode 100644 index 00000000000..65c3b65329e --- /dev/null +++ b/ext/pdo_mysql/tests/construct_persistent_failure.phpt @@ -0,0 +1,16 @@ +--TEST-- +Failure when creating persistent connection +--EXTENSIONS-- +pdo_mysql +--FILE-- + true, + ]); +} catch (PDOException $e) { + echo "Caught\n"; +} +?> +--EXPECT-- +Caught