From ffd24e01493ef58d87deda81882f9a05ec4d7ac1 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sat, 8 Feb 2025 21:33:20 +0000 Subject: [PATCH 1/2] ext/pdo_{odbc|pgsql}: Use precomputed data_source_len This is already computed by PDO, no need to recompute it again inside the drivers. --- ext/pdo_odbc/odbc_driver.c | 14 +++++++------- ext/pdo_pgsql/pgsql_driver.c | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ext/pdo_odbc/odbc_driver.c b/ext/pdo_odbc/odbc_driver.c index d84bf66ef9884..ddc9dfa25fb76 100644 --- a/ext/pdo_odbc/odbc_driver.c +++ b/ext/pdo_odbc/odbc_driver.c @@ -532,14 +532,13 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ use_direct = 1; - size_t db_len = strlen(dbh->data_source); - bool use_uid_arg = dbh->username != NULL && !php_memnistr(dbh->data_source, "uid=", strlen("uid="), dbh->data_source + db_len); - bool use_pwd_arg = dbh->password != NULL && !php_memnistr(dbh->data_source, "pwd=", strlen("pwd="), dbh->data_source + db_len); + bool use_uid_arg = dbh->username != NULL && !php_memnistr(dbh->data_source, "uid=", strlen("uid="), dbh->data_source + dbh->data_source_len); + bool use_pwd_arg = dbh->password != NULL && !php_memnistr(dbh->data_source, "pwd=", strlen("pwd="), dbh->data_source + dbh->data_source_len); if (use_uid_arg || use_pwd_arg) { - char *db = (char*) emalloc(db_len + 1); - strcpy(db, dbh->data_source); - char *db_end = db + db_len; + char *db = (char*) emalloc(dbh->data_source_len + 1); + memcpy(db, dbh->data_source, dbh->data_source_len+1); + char *db_end = db + dbh->data_source_len; db_end--; if ((unsigned char)*(db_end) == ';') { *db_end = '\0'; @@ -593,6 +592,7 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ pefree((char*)dbh->data_source, dbh->is_persistent); dbh->data_source = dsn; + dbh->data_source_len = strlen(dsn); if (uid && should_quote_uid) { efree(uid); } @@ -602,7 +602,7 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ efree(db); } - rc = SQLDriverConnect(H->dbc, NULL, (SQLCHAR *) dbh->data_source, strlen(dbh->data_source), + rc = SQLDriverConnect(H->dbc, NULL, (SQLCHAR *) dbh->data_source, dbh->data_source_len, dsnbuf, sizeof(dsnbuf)-1, &dsnbuflen, SQL_DRIVER_NOPROMPT); } if (!use_direct) { diff --git a/ext/pdo_pgsql/pgsql_driver.c b/ext/pdo_pgsql/pgsql_driver.c index 1a46cafd8f520..dd3b9535a1626 100644 --- a/ext/pdo_pgsql/pgsql_driver.c +++ b/ext/pdo_pgsql/pgsql_driver.c @@ -1396,7 +1396,7 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ /* PostgreSQL wants params in the connect string to be separated by spaces, * if the PDO standard semicolons are used, we convert them to spaces */ - e = (char *) dbh->data_source + strlen(dbh->data_source); + e = (char *) dbh->data_source + dbh->data_source_len; p = (char *) dbh->data_source; while ((p = memchr(p, ';', (e - p)))) { *p = ' '; @@ -1410,7 +1410,7 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ tmp_user = !strstr((char *) dbh->data_source, "user=") ? _pdo_pgsql_escape_credentials(dbh->username) : NULL; tmp_pass = !strstr((char *) dbh->data_source, "password=") ? _pdo_pgsql_escape_credentials(dbh->password) : NULL; - smart_str_appends(&conn_str, dbh->data_source); + smart_str_appendl(&conn_str, dbh->data_source, dbh->data_source_len); smart_str_append_printf(&conn_str, " connect_timeout=" ZEND_LONG_FMT, connect_timeout); /* support both full connection string & connection string + login and/or password */ From 12005b539d9133a07a1af8b610fa6508df4ab09f Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sun, 9 Feb 2025 00:52:11 +0000 Subject: [PATCH 2/2] Apply suggested nit --- ext/pdo_odbc/odbc_driver.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/pdo_odbc/odbc_driver.c b/ext/pdo_odbc/odbc_driver.c index ddc9dfa25fb76..7401023c573c7 100644 --- a/ext/pdo_odbc/odbc_driver.c +++ b/ext/pdo_odbc/odbc_driver.c @@ -536,8 +536,7 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ bool use_pwd_arg = dbh->password != NULL && !php_memnistr(dbh->data_source, "pwd=", strlen("pwd="), dbh->data_source + dbh->data_source_len); if (use_uid_arg || use_pwd_arg) { - char *db = (char*) emalloc(dbh->data_source_len + 1); - memcpy(db, dbh->data_source, dbh->data_source_len+1); + char *db = (char*) estrndup(dbh->data_source, dbh->data_source_len); char *db_end = db + dbh->data_source_len; db_end--; if ((unsigned char)*(db_end) == ';') {