From e0f02a75f49b4effabce4a3ca41e647366b9517e Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 27 Apr 2024 07:56:09 +0100 Subject: [PATCH] Fix GH-13519: another attempt after the faulty fix. --- ext/pgsql/pgsql.c | 13 +++++++++++++ ext/pgsql/tests/gh13519.phpt | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 ext/pgsql/tests/gh13519.phpt diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 860d007cfc01d..fdf096d8107fb 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -560,6 +560,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) /* try to find if we already have this link in our persistent list */ if ((le = zend_hash_find_ptr(&EG(persistent_list), str.s)) == NULL) { /* we don't */ +newpconn: if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) { php_error_docref(NULL, E_WARNING, "Cannot create new link. Too many open links (" ZEND_LONG_FMT ")", PGG(num_links)); @@ -591,6 +592,18 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) if (le->type != le_plink) { goto err; } + if (connect_type & PGSQL_CONNECT_FORCE_NEW) { + PGresult *pg_result; + + while ((pg_result = PQgetResult(le->ptr))) { + PQclear(pg_result); + } + PQfinish(le->ptr); + le->ptr = NULL; + PGG(num_links)--; + PGG(num_persistent)--; + goto newpconn; + } /* ensure that the link did not die */ if (PGG(auto_reset_persistent) & 1) { /* need to send & get something from backend to diff --git a/ext/pgsql/tests/gh13519.phpt b/ext/pgsql/tests/gh13519.phpt new file mode 100644 index 0000000000000..4b2e18bdd0d05 --- /dev/null +++ b/ext/pgsql/tests/gh13519.phpt @@ -0,0 +1,30 @@ +--TEST-- +GH-13519 - PGSQL_CONNECT_FORCE_NEW with persistent connections. +--EXTENSIONS-- +pgsql +--SKIPIF-- + +--FILE-- + +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(false) +bool(false) +bool(false)