Skip to content

Commit 3ab8883

Browse files
committed
Remove deprecated multi-parameter form of pg_connect()
1 parent d37d222 commit 3ab8883

File tree

3 files changed

+30
-93
lines changed

3 files changed

+30
-93
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ PHP 8.0 UPGRADE NOTES
425425
- PDO_ODBC:
426426
. The php.ini directive pdo_odbc.db2_instance_name has been removed
427427

428+
- pgsql:
429+
. The deprecated pg_connect() syntax using multiple parameters instead of a
430+
connection string is no longer supported.
431+
428432
- Phar:
429433
. Metadata associated with a phar will no longer be automatically unserialized,
430434
to fix potential security vulnerabilities due to object instantiation, autoloading, etc.

ext/pgsql/pgsql.c

Lines changed: 18 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,66 +1108,25 @@ PHP_MINFO_FUNCTION(pgsql)
11081108
/* {{{ php_pgsql_do_connect */
11091109
static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
11101110
{
1111-
char *host=NULL,*port=NULL,*options=NULL,*tty=NULL,*dbname=NULL,*connstring=NULL;
1111+
char *connstring;
1112+
size_t connstring_len;
11121113
PGconn *pgsql;
11131114
smart_str str = {0};
1114-
zval *args;
1115-
uint32_t i;
1116-
int connect_type = 0;
1115+
zend_long connect_type = 0;
11171116
PGresult *pg_result;
11181117

1119-
args = (zval *)safe_emalloc(ZEND_NUM_ARGS(), sizeof(zval), 0);
1120-
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 5
1121-
|| zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
1122-
efree(args);
1123-
WRONG_PARAM_COUNT;
1118+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &connstring, &connstring_len, &connect_type) == FAILURE) {
1119+
RETURN_THROWS();
11241120
}
11251121

11261122
smart_str_appends(&str, "pgsql");
1127-
1128-
for (i = 0; i < ZEND_NUM_ARGS(); i++) {
1129-
/* make sure that the PGSQL_CONNECT_FORCE_NEW bit is not part of the hash so that subsequent connections
1130-
* can re-use this connection. Bug #39979
1131-
*/
1132-
if (i == 1 && ZEND_NUM_ARGS() == 2 && Z_TYPE(args[i]) == IS_LONG) {
1133-
if (Z_LVAL(args[1]) == PGSQL_CONNECT_FORCE_NEW) {
1134-
continue;
1135-
} else if (Z_LVAL(args[1]) & PGSQL_CONNECT_FORCE_NEW) {
1136-
smart_str_append_long(&str, Z_LVAL(args[1]) ^ PGSQL_CONNECT_FORCE_NEW);
1137-
}
1138-
}
1139-
ZVAL_STR(&args[i], zval_get_string(&args[i]));
1140-
smart_str_appendc(&str, '_');
1141-
smart_str_appendl(&str, Z_STRVAL(args[i]), Z_STRLEN(args[i]));
1142-
}
1143-
1144-
/* Exception thrown during a string conversion. */
1145-
if (EG(exception)) {
1146-
goto cleanup;
1147-
}
1148-
1123+
smart_str_appendl(&str, connstring, connstring_len);
1124+
smart_str_appendc(&str, '_');
1125+
/* make sure that the PGSQL_CONNECT_FORCE_NEW bit is not part of the hash so that subsequent
1126+
* connections can re-use this connection. See bug #39979. */
1127+
smart_str_append_long(&str, connect_type & ~PGSQL_CONNECT_FORCE_NEW);
11491128
smart_str_0(&str);
11501129

1151-
if (ZEND_NUM_ARGS() == 1) { /* new style, using connection string */
1152-
connstring = Z_STRVAL(args[0]);
1153-
} else if (ZEND_NUM_ARGS() == 2 ) { /* Safe to add conntype_option, since 2 args was illegal */
1154-
connstring = Z_STRVAL(args[0]);
1155-
connect_type = (int)zval_get_long(&args[1]);
1156-
} else {
1157-
host = Z_STRVAL(args[0]);
1158-
port = Z_STRVAL(args[1]);
1159-
dbname = Z_STRVAL(args[ZEND_NUM_ARGS()-1]);
1160-
1161-
switch (ZEND_NUM_ARGS()) {
1162-
case 5:
1163-
tty = Z_STRVAL(args[3]);
1164-
/* fall through */
1165-
case 4:
1166-
options = Z_STRVAL(args[2]);
1167-
break;
1168-
}
1169-
}
1170-
11711130
if (persistent && PGG(allow_persistent)) {
11721131
zend_resource *le;
11731132

@@ -1185,11 +1144,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
11851144
}
11861145

11871146
/* create the link */
1188-
if (connstring) {
1189-
pgsql = PQconnectdb(connstring);
1190-
} else {
1191-
pgsql = PQsetdb(host, port, options, tty, dbname);
1192-
}
1147+
pgsql = PQconnectdb(connstring);
11931148
if (pgsql == NULL || PQstatus(pgsql) == CONNECTION_BAD) {
11941149
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql)
11951150
if (pgsql) {
@@ -1218,11 +1173,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
12181173
}
12191174
if (PQstatus(le->ptr) == CONNECTION_BAD) { /* the link died */
12201175
if (le->ptr == NULL) {
1221-
if (connstring) {
1222-
le->ptr = PQconnectdb(connstring);
1223-
} else {
1224-
le->ptr = PQsetdb(host,port,options,tty,dbname);
1225-
}
1176+
le->ptr = PQconnectdb(connstring);
12261177
}
12271178
else {
12281179
PQreset(le->ptr);
@@ -1270,25 +1221,16 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
12701221

12711222
/* Non-blocking connect */
12721223
if (connect_type & PGSQL_CONNECT_ASYNC) {
1273-
if (connstring) {
1274-
pgsql = PQconnectStart(connstring);
1275-
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
1276-
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
1277-
if (pgsql) {
1278-
PQfinish(pgsql);
1279-
}
1280-
goto err;
1224+
pgsql = PQconnectStart(connstring);
1225+
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
1226+
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
1227+
if (pgsql) {
1228+
PQfinish(pgsql);
12811229
}
1282-
} else {
1283-
php_error_docref(NULL, E_WARNING, "Connection string required for async connections");
12841230
goto err;
12851231
}
12861232
} else {
1287-
if (connstring) {
1288-
pgsql = PQconnectdb(connstring);
1289-
} else {
1290-
pgsql = PQsetdb(host,port,options,tty,dbname);
1291-
}
1233+
pgsql = PQconnectdb(connstring);
12921234
if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
12931235
PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
12941236
if (pgsql) {
@@ -1324,18 +1266,10 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
13241266
php_pgsql_set_default_link(Z_RES_P(return_value));
13251267

13261268
cleanup:
1327-
for (i = 0; i < ZEND_NUM_ARGS(); i++) {
1328-
zval_ptr_dtor(&args[i]);
1329-
}
1330-
efree(args);
13311269
smart_str_free(&str);
13321270
return;
13331271

13341272
err:
1335-
for (i = 0; i < ZEND_NUM_ARGS(); i++) {
1336-
zval_ptr_dtor(&args[i]);
1337-
}
1338-
efree(args);
13391273
smart_str_free(&str);
13401274
RETURN_FALSE;
13411275
}

ext/pgsql/tests/bug72195.phpt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ Bug #72195 (pg_pconnect/pg_connect cause use-after-free)
55
--FILE--
66
<?php
77
$val = [];
8-
$var1 = $val;
9-
printf("%x\n", count($val));
10-
@pg_pconnect($var1, "2", "3", "4");
11-
$var1 = "";
12-
tempnam(sys_get_temp_dir(), 'ABCDEFGHI');
13-
printf("%x\n", count($val));
8+
try {
9+
pg_pconnect($var1, "2", "3", "4");
10+
} catch (ArgumentCountError $e) {
11+
echo $e->getMessage(), "\n";
12+
}
1413
?>
15-
--EXPECT--
16-
0
17-
0
14+
--EXPECTF--
15+
Warning: Undefined variable $var1 in %s on line %d
16+
pg_pconnect() expects at most 2 arguments, 4 given

0 commit comments

Comments
 (0)