Skip to content

Commit b757fa8

Browse files
authored
ext/pdo_{odbc|pgsql}: Use precomputed data_source_len (#17744)
This is already computed by PDO, no need to recompute it again inside the drivers.
1 parent 4e55889 commit b757fa8

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

ext/pdo_odbc/odbc_driver.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,12 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
532532

533533
use_direct = 1;
534534

535-
size_t db_len = strlen(dbh->data_source);
536-
bool use_uid_arg = dbh->username != NULL && !php_memnistr(dbh->data_source, "uid=", strlen("uid="), dbh->data_source + db_len);
537-
bool use_pwd_arg = dbh->password != NULL && !php_memnistr(dbh->data_source, "pwd=", strlen("pwd="), dbh->data_source + db_len);
535+
bool use_uid_arg = dbh->username != NULL && !php_memnistr(dbh->data_source, "uid=", strlen("uid="), dbh->data_source + dbh->data_source_len);
536+
bool use_pwd_arg = dbh->password != NULL && !php_memnistr(dbh->data_source, "pwd=", strlen("pwd="), dbh->data_source + dbh->data_source_len);
538537

539538
if (use_uid_arg || use_pwd_arg) {
540-
char *db = (char*) emalloc(db_len + 1);
541-
strcpy(db, dbh->data_source);
542-
char *db_end = db + db_len;
539+
char *db = (char*) estrndup(dbh->data_source, dbh->data_source_len);
540+
char *db_end = db + dbh->data_source_len;
543541
db_end--;
544542
if ((unsigned char)*(db_end) == ';') {
545543
*db_end = '\0';
@@ -593,6 +591,7 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
593591

594592
pefree((char*)dbh->data_source, dbh->is_persistent);
595593
dbh->data_source = dsn;
594+
dbh->data_source_len = strlen(dsn);
596595
if (uid && should_quote_uid) {
597596
efree(uid);
598597
}
@@ -602,7 +601,7 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
602601
efree(db);
603602
}
604603

605-
rc = SQLDriverConnect(H->dbc, NULL, (SQLCHAR *) dbh->data_source, strlen(dbh->data_source),
604+
rc = SQLDriverConnect(H->dbc, NULL, (SQLCHAR *) dbh->data_source, dbh->data_source_len,
606605
dsnbuf, sizeof(dsnbuf)-1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
607606
}
608607
if (!use_direct) {

ext/pdo_pgsql/pgsql_driver.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
13961396
/* PostgreSQL wants params in the connect string to be separated by spaces,
13971397
* if the PDO standard semicolons are used, we convert them to spaces
13981398
*/
1399-
e = (char *) dbh->data_source + strlen(dbh->data_source);
1399+
e = (char *) dbh->data_source + dbh->data_source_len;
14001400
p = (char *) dbh->data_source;
14011401
while ((p = memchr(p, ';', (e - p)))) {
14021402
*p = ' ';
@@ -1410,7 +1410,7 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
14101410
tmp_user = !strstr((char *) dbh->data_source, "user=") ? _pdo_pgsql_escape_credentials(dbh->username) : NULL;
14111411
tmp_pass = !strstr((char *) dbh->data_source, "password=") ? _pdo_pgsql_escape_credentials(dbh->password) : NULL;
14121412

1413-
smart_str_appends(&conn_str, dbh->data_source);
1413+
smart_str_appendl(&conn_str, dbh->data_source, dbh->data_source_len);
14141414
smart_str_append_printf(&conn_str, " connect_timeout=" ZEND_LONG_FMT, connect_timeout);
14151415

14161416
/* support both full connection string & connection string + login and/or password */

0 commit comments

Comments
 (0)