From 49b0f075948674dbb8e955d0ce6b8a2fd6a2bd2e Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 26 Feb 2024 19:36:42 +0000 Subject: [PATCH 1/2] Fix GH-13519: PGSQL_CONNECT_FORCE_RENEW with persistent connections. persistent connections did not take in account this flag, after the usual link sanity checks, we remove its entry. --- ext/pgsql/pgsql.c | 7 +++++++ ext/pgsql/tests/gh13519.phpt | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 ext/pgsql/tests/gh13519.phpt diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 6e04848bdea0b..0e0801bf430e7 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -562,6 +562,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)); @@ -620,6 +621,12 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) pg_result = PQexec(pgsql, "RESET ALL;"); PQclear(pg_result); } + if ((connect_type & PGSQL_CONNECT_FORCE_NEW)) { + if (zend_hash_del(&EG(persistent_list), str.s) != SUCCESS) { + goto err; + } + goto newpconn; + } } object_init_ex(return_value, pgsql_link_ce); diff --git a/ext/pgsql/tests/gh13519.phpt b/ext/pgsql/tests/gh13519.phpt new file mode 100644 index 0000000000000..17379cdacce71 --- /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) From 32fc0bf148906ca87fe28950ca4729f0c974f880 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 26 Feb 2024 23:55:08 +0000 Subject: [PATCH 2/2] just remove from the hashtable earlier. --- ext/pgsql/pgsql.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 0e0801bf430e7..9b2488a5eb889 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -591,6 +591,12 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) PGG(num_links)++; PGG(num_persistent)++; } else { /* we do */ + if ((connect_type & PGSQL_CONNECT_FORCE_NEW)) { + if (zend_hash_del(&EG(persistent_list), str.s) != SUCCESS) { + goto err; + } + goto newpconn; + } if (le->type != le_plink) { goto err; } @@ -621,12 +627,6 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) pg_result = PQexec(pgsql, "RESET ALL;"); PQclear(pg_result); } - if ((connect_type & PGSQL_CONNECT_FORCE_NEW)) { - if (zend_hash_del(&EG(persistent_list), str.s) != SUCCESS) { - goto err; - } - goto newpconn; - } } object_init_ex(return_value, pgsql_link_ce);