Skip to content

Commit 6c8a71b

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix GH-21162: pg_connect() on error memory leak.
2 parents aae3002 + 539c5d9 commit 6c8a71b

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

ext/pgsql/pgsql.c

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

@@ -790,19 +792,23 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
790792
if (connect_type & PGSQL_CONNECT_ASYNC) {
791793
pgsql = PQconnectStart(connstring);
792794
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
793-
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
795+
zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql));
794796
if (pgsql) {
795797
PQfinish(pgsql);
796798
}
799+
php_error_docref(NULL, E_WARNING, "Unable to connect to PostgreSQL server: %s", ZSTR_VAL(msgbuf));
800+
zend_string_release(msgbuf);
797801
goto err;
798802
}
799803
} else {
800804
pgsql = PQconnectdb(connstring);
801805
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
802-
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
806+
zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql));
803807
if (pgsql) {
804808
PQfinish(pgsql);
805809
}
810+
php_error_docref(NULL, E_WARNING, "Unable to connect to PostgreSQL server: %s", ZSTR_VAL(msgbuf));
811+
zend_string_release(msgbuf);
806812
goto err;
807813
}
808814
}

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)