Skip to content

Commit 539c5d9

Browse files
committed
Fix GH-21162: pg_connect() on error memory leak.
The PHP_PQ_ERROR macro calls php_error_docref() which triggers user error handlers thus libpq does not have the chance to clean the resources (and empty connections string are allowed) on failure thus we avoid this macro and delay the error handling after. close GH-21165
1 parent 901ebd2 commit 539c5d9

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ PHP NEWS
3434
. Fixed bug GH-21055 (connection attribute status typo for GSS negotiation).
3535
(lsaos)
3636

37+
- PGSQL:
38+
. Fixed bug GH-21162 (pg_connect() memory leak on error).
39+
(David Carlier)
40+
3741
- Sockets:
3842
. Fixed bug GH-21161 (socket_set_option() crash with array 'addr'
3943
entry as null). (David Carlier)

ext/pgsql/pgsql.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,12 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
705705
/* create the link */
706706
pgsql = PQconnectdb(connstring);
707707
if (pgsql == NULL || PQstatus(pgsql) == CONNECTION_BAD) {
708-
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql)
708+
zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql));
709709
if (pgsql) {
710710
PQfinish(pgsql);
711711
}
712+
php_error_docref(NULL, E_WARNING, "Unable to connect to PostgreSQL server: %s", ZSTR_VAL(msgbuf));
713+
zend_string_release(msgbuf);
712714
goto err;
713715
}
714716

@@ -789,19 +791,23 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
789791
if (connect_type & PGSQL_CONNECT_ASYNC) {
790792
pgsql = PQconnectStart(connstring);
791793
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
792-
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
794+
zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql));
793795
if (pgsql) {
794796
PQfinish(pgsql);
795797
}
798+
php_error_docref(NULL, E_WARNING, "Unable to connect to PostgreSQL server: %s", ZSTR_VAL(msgbuf));
799+
zend_string_release(msgbuf);
796800
goto err;
797801
}
798802
} else {
799803
pgsql = PQconnectdb(connstring);
800804
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
801-
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
805+
zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql));
802806
if (pgsql) {
803807
PQfinish(pgsql);
804808
}
809+
php_error_docref(NULL, E_WARNING, "Unable to connect to PostgreSQL server: %s", ZSTR_VAL(msgbuf));
810+
zend_string_release(msgbuf);
805811
goto err;
806812
}
807813
}

ext/pgsql/tests/gh21162.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-21162 (pg_connect() on error memory leak)
3+
--EXTENSIONS--
4+
pgsql
5+
--FILE--
6+
<?php
7+
8+
set_error_handler(function (int $errno, string $errstr) {
9+
echo "Warning caught\n";
10+
});
11+
12+
pg_connect('');
13+
14+
echo "Done\n";
15+
?>
16+
--EXPECT--
17+
Warning caught
18+
Done

0 commit comments

Comments
 (0)