Skip to content

Commit 54e5e7b

Browse files
authored
ext/pdo_pgsql: object initialisation, using smart_str api instead. (#14679)
1 parent 604daff commit 54e5e7b

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

ext/pdo_pgsql/pgsql_driver.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "php_pdo_pgsql.h"
3232
#include "php_pdo_pgsql_int.h"
3333
#include "zend_exceptions.h"
34+
#include "zend_smart_str.h"
3435
#include "pgsql_driver_arginfo.h"
3536

3637
static bool pgsql_handle_in_transaction(pdo_dbh_t *dbh);
@@ -1329,8 +1330,9 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
13291330
{
13301331
pdo_pgsql_db_handle *H;
13311332
int ret = 0;
1332-
char *conn_str, *p, *e;
1333+
char *p, *e;
13331334
zend_string *tmp_user, *tmp_pass;
1335+
smart_str conn_str = {0};
13341336
zend_long connect_timeout = 30;
13351337

13361338
H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
@@ -1361,18 +1363,20 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
13611363
tmp_user = !strstr((char *) dbh->data_source, "user=") ? _pdo_pgsql_escape_credentials(dbh->username) : NULL;
13621364
tmp_pass = !strstr((char *) dbh->data_source, "password=") ? _pdo_pgsql_escape_credentials(dbh->password) : NULL;
13631365

1366+
smart_str_appends(&conn_str, dbh->data_source);
1367+
smart_str_append_printf(&conn_str, " connect_timeout=" ZEND_LONG_FMT, connect_timeout);
1368+
13641369
/* support both full connection string & connection string + login and/or password */
1365-
if (tmp_user && tmp_pass) {
1366-
spprintf(&conn_str, 0, "%s user='%s' password='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_user), ZSTR_VAL(tmp_pass), connect_timeout);
1367-
} else if (tmp_user) {
1368-
spprintf(&conn_str, 0, "%s user='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_user), connect_timeout);
1369-
} else if (tmp_pass) {
1370-
spprintf(&conn_str, 0, "%s password='%s' connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, ZSTR_VAL(tmp_pass), connect_timeout);
1371-
} else {
1372-
spprintf(&conn_str, 0, "%s connect_timeout=" ZEND_LONG_FMT, (char *) dbh->data_source, connect_timeout);
1370+
if (tmp_user) {
1371+
smart_str_append_printf(&conn_str, " user='%s'", ZSTR_VAL(tmp_user));
1372+
}
1373+
1374+
if (tmp_pass) {
1375+
smart_str_append_printf(&conn_str, " password='%s'", ZSTR_VAL(tmp_pass));
13731376
}
1377+
smart_str_0(&conn_str);
13741378

1375-
H->server = PQconnectdb(conn_str);
1379+
H->server = PQconnectdb(ZSTR_VAL(conn_str.s));
13761380
H->lob_streams = (HashTable *) pemalloc(sizeof(HashTable), dbh->is_persistent);
13771381
zend_hash_init(H->lob_streams, 0, NULL, NULL, 1);
13781382

@@ -1383,7 +1387,7 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
13831387
zend_string_release_ex(tmp_pass, 0);
13841388
}
13851389

1386-
efree(conn_str);
1390+
smart_str_free(&conn_str);
13871391

13881392
if (PQstatus(H->server) != CONNECTION_OK) {
13891393
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);

0 commit comments

Comments
 (0)