31
31
#include "php_pdo_pgsql.h"
32
32
#include "php_pdo_pgsql_int.h"
33
33
#include "zend_exceptions.h"
34
+ #include "zend_smart_str.h"
34
35
#include "pgsql_driver_arginfo.h"
35
36
36
37
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) /* {{{
1329
1330
{
1330
1331
pdo_pgsql_db_handle * H ;
1331
1332
int ret = 0 ;
1332
- char * conn_str , * p , * e ;
1333
+ char * p , * e ;
1333
1334
zend_string * tmp_user , * tmp_pass ;
1335
+ smart_str conn_str = {0 };
1334
1336
zend_long connect_timeout = 30 ;
1335
1337
1336
1338
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) /* {{{
1361
1363
tmp_user = !strstr ((char * ) dbh -> data_source , "user=" ) ? _pdo_pgsql_escape_credentials (dbh -> username ) : NULL ;
1362
1364
tmp_pass = !strstr ((char * ) dbh -> data_source , "password=" ) ? _pdo_pgsql_escape_credentials (dbh -> password ) : NULL ;
1363
1365
1366
+ smart_str_appends (& conn_str , dbh -> data_source );
1367
+ smart_str_append_printf (& conn_str , " connect_timeout=" ZEND_LONG_FMT , connect_timeout );
1368
+
1364
1369
/* 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 ));
1373
1376
}
1377
+ smart_str_0 (& conn_str );
1374
1378
1375
- H -> server = PQconnectdb (conn_str );
1379
+ H -> server = PQconnectdb (ZSTR_VAL ( conn_str . s ) );
1376
1380
H -> lob_streams = (HashTable * ) pemalloc (sizeof (HashTable ), dbh -> is_persistent );
1377
1381
zend_hash_init (H -> lob_streams , 0 , NULL , NULL , 1 );
1378
1382
@@ -1383,7 +1387,7 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
1383
1387
zend_string_release_ex (tmp_pass , 0 );
1384
1388
}
1385
1389
1386
- efree ( conn_str );
1390
+ smart_str_free ( & conn_str );
1387
1391
1388
1392
if (PQstatus (H -> server ) != CONNECTION_OK ) {
1389
1393
pdo_pgsql_error (dbh , PGRES_FATAL_ERROR , PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE );
0 commit comments