diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 80aacd5443eaa..99f2e3cf563fe 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -705,10 +705,12 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) /* create the link */ pgsql = PQconnectdb(connstring); if (pgsql == NULL || PQstatus(pgsql) == CONNECTION_BAD) { - PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql) + zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql)); if (pgsql) { PQfinish(pgsql); } + php_error_docref(NULL, E_WARNING, "Unable to connect to PostgreSQL server: %s", ZSTR_VAL(msgbuf)); + zend_string_release(msgbuf); goto err; } @@ -789,19 +791,23 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) if (connect_type & PGSQL_CONNECT_ASYNC) { pgsql = PQconnectStart(connstring); if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) { - PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql); + zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql)); if (pgsql) { PQfinish(pgsql); } + php_error_docref(NULL, E_WARNING, "Unable to connect to PostgreSQL server: %s", ZSTR_VAL(msgbuf)); + zend_string_release(msgbuf); goto err; } } else { pgsql = PQconnectdb(connstring); if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) { - PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql); + zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql)); if (pgsql) { PQfinish(pgsql); } + php_error_docref(NULL, E_WARNING, "Unable to connect to PostgreSQL server: %s", ZSTR_VAL(msgbuf)); + zend_string_release(msgbuf); goto err; } } diff --git a/ext/pgsql/tests/gh21162.phpt b/ext/pgsql/tests/gh21162.phpt new file mode 100644 index 0000000000000..9f40ac1136d3c --- /dev/null +++ b/ext/pgsql/tests/gh21162.phpt @@ -0,0 +1,18 @@ +--TEST-- +GH-21162 (pg_connect() on error memory leak) +--EXTENSIONS-- +pgsql +--FILE-- + +--EXPECT-- +Warning caught +Done